Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do we put node modules we install by npm in a Meteor project?

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

I installed my node modules via npm from inside /public/ and therefore I have a /public/node_modules/ directory.

I don't think this is the 'proper' or 'standard' place for them because according to the Meteor docs...

Meteor gathers all your JavaScript files, excluding anything under the client and public subdirectories, and loads them into a Node.js server instance inside a fiber

The code to load is in the server dir and server js files 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 standard and I don't believe I should be doing it this way. Yet, it works both on my dev platform and in production at deploy meteor.com.

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

like image 560
Steeve Cannon Avatar asked May 14 '12 17:05

Steeve Cannon


People also ask

Where should node modules be installed?

Node Modules Global installs on Unix systems go to {prefix}/lib/node_modules . Global installs on Windows go to {prefix}/node_modules (that is, no lib folder.)

Where does npm install its modules?

Path of Global Packages in the system: Global modules are installed in the standard system in root location in system directory /usr/local/lib/node_modules project directory.

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 .

Where does npm install node?

Node Packaged Modules npm can install packages in local or global mode. In local mode, it installs the package in a node_modules folder in your parent working directory. This location is owned by the current user.


2 Answers

cd /usr/local/meteor/lib/ && npm install <module>
like image 155
Ian Wojtowicz Avatar answered Sep 21 '22 05:09

Ian Wojtowicz


To use Npm modules in Meteor its adding the npm module in.

First you need to add a npm package adapter such as meteorhacks:npm

meteor add meteorhacks:npm

Then start your meteor app by running meteor, you will notice a new packages.json file in your project

Add in modules like this (you need to explicitly define a version)

{
    "request" : "2.53.0"
}

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

var request = Meteor.npmRequire("request")
like image 39
Tarang Avatar answered Sep 22 '22 05:09

Tarang