Where do we put the node modules that we install on npm in a Meteor project?

I followed the github meteorirc project project and put them in / public /

I installed my node modules through npm from inside / public /, and so I have a directory / public / node_modules /.

I don’t think this is the “right” or “standard” place for them, because according to the Meteor docs ...

Meteor collects all your JavaScript files except client and public subdirectories and uploads them to the Node.js server instance inside the fiber

The download code is in the dir and server files of the js server and looks like this.

var require = __meteor_bootstrap__.require; var path = require("path"); var fs = require('fs'); var base = path.resolve('.'); if (base == '/'){ base = path.dirname(global.require.main.filename); } var Twit; var twitPath = 'node_modules/twit'; var publicTwitPath = path.resolve(base+'/public/'+twitPath); var staticTwitPath = path.resolve(base+'/static/'+twitPath); if (path.existsSync(publicTwitPath)){ Twit = require(publicTwitPath); } else if (path.existsSync(staticTwitPath)){ Twit = require(staticTwitPath); } else{ console.log('WARNING Twit not loaded. Node_modules not found'); } 

Based on the docs, this is not a standard, and I don't think I should do it that way. However, it works both on my dev platform and in production when deploying meteor.com.

Where should node modules be installed in the project directory structure so that they work locally and after deployment on meteor.com or elsewhere?

+10
javascript meteor
May 14 '12 at 17:08
source share
6 answers
 cd /usr/local/meteor/lib/ && npm install <module> 
+6
May 21 '12 at 9:53 a.m.
source share

To use Npm modules in Meteor, add the npm module.

First you need to add an npm package adapter, for example meteorhacks:npm

 meteor add meteorhacks:npm 

Then run the meteor application, running meteor , you will see a new packages.json file in your project

Add to modules like this (you need to explicitly specify the version)

 { "request" : "2.53.0" } 

Then you can use npm modules in your meteor application, use Meteor.npmRequire instead of require

 var request = Meteor.npmRequire("request") 
+2
Feb 09 '15 at 13:22
source share

Meteor takes lib/node_modules from the development package and creates a symbolic link or copies it to server/node_modules , which is located in a hidden subfolder of .meteor in your project.

So, if you are in the lib directory of the development package or in the server directory of the .meteor folder (I believe that it is in build ); you can use node modules. If you have problems downloading them, you can check this question .

+1
May 15 '12 at 16:28
source share

You need to add the bundle folder to the path:

 var staticTwitPath = path.resolve(base+'/bundle/static/'+twitPath); 

Here is my working sample in coffeescript , node_modules is in the public folder:

 # loading node_modules from public folder require = __meteor_bootstrap__.require path = require("path") fs = require('fs') cheerioPath = 'node_modules/cheerio' base = path.resolve('.') if base == '/' base = path.dirname(global.require.main.filename) publicPath = path.resolve(base+'/public/'+cheerioPath) staticPath = path.resolve(base+'/bundle/static/'+cheerioPath) if path.existsSync(publicPath) cheerio = require(publicPath) else if path.existsSync(staticPath) cheerio = require(staticPath) else console.log('node_modules not found') 

Good luck

+1
Aug 28 2018-12-12T00:
source share

It helped me a lot, including the syntax highlighting package! Thank!

I use a little helper, although, as I think, this will not be the last npm package I will use;)

 meteorNpm = do() -> require = __meteor_bootstrap__.require path = require 'path' fs = require 'fs' base = path.resolve '.' if base is '/' base = path.dirname global.require.main.filename meteorNpm = # requires npm modules placed in `public/node_modules` require: (moduleName) -> modulePath = 'node_modules/' + moduleName publicPath = path.resolve(base + '/public/' + modulePath) staticPath = path.resolve(base + '/bundle/static/' + modulePath) if path.existsSync(publicPath) module = require publicPath else if path.existsSync(staticPath) module = require staticPath else module = null return module 

Use it as follows:

 highlight = meteorNpm.require "highlight.js" 
0
Aug 31 '12 at 11:38
source share

I use a script that perfectly installs all the dependencies of node.js. It behaves similarly to official support in the Meteor engine branch (it installs dependencies at runtime), but it also supports installation from git repositories and similar properties.

0
Mar 19 '13 at 0:53
source share



All Articles