8

With the official launch of Meteor, is there a solid way to use NPM packages? I'm trying to use embed.ly but I don't see any straightforward way to do so.

Also, as a meteor novice, how do I include packages in my files? I don't see any 'require' or 'exports' functions.

Thanks!

kaid
  • 1,249
  • 2
  • 16
  • 32
  • 1
    When you add a Meteor package to your app, its exports are automatically accessible in your JS files. No need to use require. – stubailo Oct 30 '14 at 18:57
  • 1
    I'd hate to close a Meteor question, but this is an exact duplicate of [How do we or can we use node modules via npm with Meteor?](http://stackoverflow.com/questions/10165978/how-do-we-or-can-we-use-node-modules-via-npm-with-meteor) – Dan Dascalescu Jan 04 '15 at 22:11

3 Answers3

10

In the new "localmarket" example, they include a npm package in the package directory like this:

Request = Meteor.wrapAsync(Npm.require('request'));

and in the package.js file:

Package.describe({
  summary: "Wraps the request module from Npm in a fiber.",
  version: '0.0.0'
});

Npm.depends({request: "2.33.0"});

Package.on_use(function (api) {
  api.add_files('request-server.js', 'server');
  api.export('Request');
});
Francis
  • 691
  • 6
  • 7
  • 2
    the question wasn't about use of npm packages in meteor packages. The mechanism to use npm packages there is different from use in a meteor project itself – Christian Fritz Oct 30 '14 at 23:34
  • I agree, this seems to me the cleaner approach, because using already implemented features. Instead of adding a 3rd party package, which enables you to include npm packages through yet another config file, you can simply create your own package and define dependencies. – udondan Oct 31 '14 at 04:14
8

You can install meteorhacks:npm

meteor add meteorhacks:npm
meteor

Meteor will then stop. You can then edit the new package.json file

{
    "request" : "2.33.0"
}

Then when you start Meteor it will install the npm modules for you.

Usage would as follows (use Meteor.npmRequire instead of require)

request = Meteor.npmRequire("request");
Tarang
  • 75,157
  • 39
  • 215
  • 276
  • 1
    It's worth noting that Meteor.npmRequire is server side only. https://github.com/meteorhacks/npm – bitsoflogic Dec 24 '14 at 19:49
  • hi there, thank you for the answer! I'm now running into some problems with using the JSFTP module in particular. I have a question here, I'd appreciate it if you could give me some pointers. http://stackoverflow.com/questions/28822711/running-ftp-in-meteor – kaid Mar 03 '15 at 19:11
  • Also worth noting Meteor Supports this natively: http://docs.meteor.com/#/full/Npm-depends – jmunsch Jun 24 '15 at 16:50
  • 2
    @jm_____ The `meteorhacks:npm` package is actually just a proxy for Npm.depends and Npm.require. It's not straightforward to use natively as it requires you to make a package containing them, as meteorhacks:npm does without you having to write one. – Tarang Jun 24 '15 at 17:22
1

You can install this package https://github.com/meteorhacks/npm first. Then use it to require other NPM packages.

waitingkuo
  • 89,478
  • 28
  • 112
  • 118