Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we or can we use node modules via npm with Meteor?

How do we or can we use node modules via npm with Meteor?

Or is that something that will be dependent on the packaging API?

Or is there a prescribed method that is recommended?

like image 439
Steeve Cannon Avatar asked Apr 15 '12 20:04

Steeve Cannon


People also ask

How do I use npm on Meteor js?

To use an npm package from a file in your application you import the name of the package: import moment from 'moment'; // this is equivalent to the standard node require: const moment = require('moment'); This imports the default export from the package into the symbol moment .

What is Meteor in npm?

Meteor is a full-stack JavaScript platform for developing modern web and mobile applications. Meteor includes a key set of technologies for building connected-client reactive applications, a build tool, and a curated set of packages from the Node. js and general JavaScript community.

Can you use node packages with Deno?

js has a plugin system that is incompatible with Deno, and Deno will never support Node. js plugins. If the Node. js code you want to use requires a "native" Node.


2 Answers

Meteor 1.3, released on March 28, 2016, gives apps full ES6 (ES2015) modules support and out of the box NPM support. Apps and packages can now load NPM modules directly and easily on the client and on the server.

If you can use 1.3, then check http://guide.meteor.com/using-packages.html#installing-npm.

For example, to use moment.js:

meteor npm install --save moment 

Then in your code:

import moment from 'moment';  // this is equivalent to the standard node require: const moment = require('moment'); 

If you need to use an older version of Meteor, read the rest of the answer below.


Pre-Meteor 1.3:

Since v0.6.0, Meteor integrates directly with NPM modules with the help of a 3rd party package. For example, to use a module like ws,

  1. Run sudo npm install -g ws (or for local installs, see this)
  2. In your sever JavaScript file,

    var Websocket = Npm.require('ws'); var myws = new Websocket('url'); 

To use a core Node module, just make the corresponding Npm.require() call, e.g. var Readable = Npm.require('stream').Readable.


You can use any of the more than 230,000 NPM modules directly with Meteor thanks to the NPM package developed by Arunoda.

You can also define dependencies on Npm packages from smart packages - from the initial announcement of npm support:

Your smart package can now define dependencies directly, by adding a call to Npm.depends in package.js:

Npm.depends({   "awssum": "0.12.2",   "underscore.string": "2.3.1" }); 

All of this works well with hot code reload, just like the rest of Meteor. When you make changes, the bundler will automatically download missing npm packages and re-pin its dependencies.

To use an NPM module within server code, use Npm.require as you would normally use plain require. Notably, __meteor_bootstrap__.require has been eliminated and all of its uses have been converted to Npm.require.

There is a small example of using an NPM module in your application.

like image 108
Dan Dascalescu Avatar answered Sep 21 '22 05:09

Dan Dascalescu


Note that this answer applies to versions of Meteor prior to 0.6.0, which was released in April 2013 and added direct npm integration

Install modules as you normally would through npm and then use

var require = __meteor_bootstrap__.require,     pd = require("pd"),     after = require("after") // etc 

Load any modules you want

like image 35
Raynos Avatar answered Sep 23 '22 05:09

Raynos