Using NPM packages syntax


#1

I sometimes have trouble using NPM modules in my Meteor projects and I’d like to know more about this topic. I’ve read the docs, but I still have some basic questions.

  1. Can all NPM modules be used with Meteor?
  2. If so, should I always be able to use the ES6 import statement to do so, or do I sometimes have to rely on require based on how the package itself is written?
  3. Is the syntax always the same for the import statement? import Object from 'package'? If not, how do I know what statement to use?

Thanks for your help!


#2

There’s an awful lot of weird and wacky stuff you can put in NPM modules, I doubt anyone will say ‘all’ will work, but there are very few issues so give it a try with the modules you’re interested in.

You should be able to use the ES6 import for pretty much everything. The docs give a number of examples: https://guide.meteor.com/using-npm-packages.html

The same page mentions the problem of binding environment when using asynchronous callbacks in code used on the server.

The package you are using will usually give some sort of instructions on how to consume, and as there are a few variations that’s the best way of finding out - obviously if the package has some example code, that is likely to help too.

Here’s a nice tutorial from Ryan Glover which may be worth a read too: https://themeteorchef.com/tutorials/using-npm-packages


#3

I believe so unless they are built for Node 6 or 8, and that won’t last much longer with 1.6 on the way, which upgrades to Node 8.

The biggest difficulty is that it may be hard to tell if a module is for the client or server. For instance, the aws-sdk module requires Buffer, which is no longer on the client by default, and so is easiest to use on the server.


#4

Thanks for the feedback.

Indeed, really useful read for the topic at hand. It answers most of my questions, so thanks a lot for the reference! I learned a few nice things here and there.

Also good to know. I might avoid some struggle if I ever feel like banging my head on the wall for incomprehensible errors! :stuck_out_tongue_winking_eye: