简体   繁体   中英

Do I have to import npm packages in every file with Meteor and set options in every file?

I'm trying to get used to using npm packages brought in to Meteor 1.3 alongside regular Atmosphere packages. I have been able to use the slug package by using

meteor npm install slug

Then in one of my .js files I import slug with this command

import slug from 'slug';

And it seems to work. But it doesn't work when I try it from another .js file. Do I have to put the import command at the top of every file I want to use it in? Is this loading it multiple times in memory?

I'm also changing the default options using

slug.defaults.mode ='rfc3986';

And I'm wondering if I need to put that at the top of all my files as well. Atmosphere packages were a lot simpler. You simply added them and then you could use them throughout the whole project.

If you don't want to repeat the options, use this pattern:

Create a /lib/slug.js in your Meteor project with this content:

import slug from 'slug';

slug.defaults.mode = 'rfc3986';

export default slug;

Then throughout your project do not import slug from 'slug'; but rather import slug from '/lib/slug'; .

Yes, you do have to import the module to another module to make it available there. Everything inside the module will not be available to other modules unless you import that module to each module. Please notice the keyword.

Yes you have to import here are some benefits of using imports:

  • You can control the load order of files by encoding dependencies through imports.
  • You can create reusable “modules”.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM