Lt.
Lt.

Reputation: 1268

Use npm in Meteor packages

Was trying to figure out the way to load a npm package into a Meteor one. Specifically I tried with future-npm

I've tried to do this:

Package.describe({
  summary: "Blah blah",
  version: '0.0.1'
});

Npm.depends({future: "2.3.1"});

Package.onUse(function (api) {
  api.addFiles('lubert.js', 'server');
  api.export('Lubert');
});

Unfortunately I got the following console error

 Uncaught ReferenceError: Npm is not defined

I've read the documentation and there is nothing about to load any dependency

What am I doing wrong?

Update 2: My package.js looks like

Package.describe({
  name: 'trepafi:package',
  summary: '',
  version: '0.0.3',
  git: 'https://github.com/trepafi/meteor-package.git'
});

Npm.depends({
  "future": "2.3.1"
});

Package.onUse(function(api) {
  api.versionsFrom('1.0');
  api.use(['tracker', 'underscore'], ['client']);
  api.addFiles(['package.js'], ['client']);
  api.export('Package', ['client']);
});

Update 1: My package.json looks like

{
  "name": "trepafi-package",
  "version": "0.0.3",
  "description": "Package for Meteor",
  "repository": {
    "type": "git",
    "url": "https://github.com/trepafi/meteor-package.git"
  },
  "author": "Lubert Palacios",
  "license": "MIT",
  "homepage": "https://github.com/trepafi/meteor-package",
  "dependencies": {
    "future": "^2.3.1"
  }
}

I've also tried with meteorhacks:npm without success. It'd be nice if I can use a "native" way

Upvotes: 3

Views: 1742

Answers (1)

Seth Malaki
Seth Malaki

Reputation: 4486

You should be grouping all Npm.requires at the end of your package.js file.

For future-npm. You don't need Npm.depends in package.js, Silly. It's already included in meteor.. just Npm.require somewhere and you're good to go. To do that:

  1. Don't be ambiguous with the package.js name. Use the trepafi:package.js instead.

  2. You don't need package.json.. Npm.depends has got you covered.

  3. Remove this: api.addFiles(['package.js'], ['client']); as it looks like a circular dependency.. Yo dawg i herd you liek package.js in you package.js.. not cool, Xzibit.

  4. And since Npm.require only works server-side, you need to include trepafi:package.js as server-side. e.g:

    api.addFiles(['trepafi:package.js'], ['server']);

So your structure should at least be:

trepafi:package/
  - package.js
  - trepafi:package.js
  - <other files..>

You don't need package.json for Future.. it's already included with Meteor.

Your package.js should look like:

Package.describe({
  name: 'trepafi:package',
  summary: '',
  version: '0.0.3',
  git: 'https://github.com/trepafi/meteor-package.git'
});

Package.onUse(function(api) {
  api.versionsFrom('[email protected]');
  api.use(['tracker', 'underscore','meteor'], ['client']);
  api.addFiles(['trepafi:package.js'], ['server']);
  api.export('Package', ['client']);
});

//if you really need Npm.depends:

Npm.depends({
   'prerender-node': '1.0.6',
   'send' : '0.10.1'
});

// we don't need no package.json

Your trepafi:package.js should look like:

var Future = Npm.require('future');
var future = new Future();

// use your future to make Doc Brown proud.

var useFuture = function(asyncFunc) { //expects function with callback somewhere
    asyncFunc(function(err, result) {
        if(err) future.throw("OMG something went wrong!");
        else return future.return(result);
    });
    return future.wait();
};

Meteor.startup(function() {
   //something      
});

READ:

Notable changes in package.js are versionsFrom and api.use now has meteor added to it.

Good luck!

Upvotes: 5

Related Questions