How can I deploy node modules in a Meteor app on meteor.com?
As of Meteor 6.0, now we need to use Npm.require() instead. Additionally, we need to declare the module as global variables, since Meteor now has file-level scope.
var path = Npm.require('path');
var fs = Npm.require('fs');
var base = path.resolve('.');
var isBundle = fs.existsSync(base + '/bundle');
var modulePath = base + (isBundle ? '/bundle/static' : '/public') + '/node_modules';
MODULE_NAME = Npm.require(modulePath + '/MODULE_NAME'); // NOTE, this is going to be a global variable
finally, I wrote like this. it works both in local and meteor sever. thx Ian :D
install npm module inside "app/public":
app/public# npm install MODULE_NAME
inside app/server/server.js:
Meteor.startup(function () {
var require = __meteor_bootstrap__.require;
var path = require('path');
var base = path.resolve('.');
var isBundle = path.existsSync(base + '/bundle');
var modulePath = base + (isBundle ? '/bundle/static' : '/public') + '/node_modules';
var MODULE_NAME = require(modulePath + '/MODULE_NAME');
});
Answer found from JonathanKingston on meteor irc. Referred to meteoric project
Put node modules in the projects public directory.
Use code like this to make sure it loads.
var require = __meteor_bootstrap__.require;
var path = require("path");
var fs = require('fs');
var Twit;
var twitPath = 'node_modules/twit';
var base = path.resolve('.');
if (base == '/'){
base = path.dirname(global.require.main.filename);
}
var publicPath = path.resolve(base+'/public/'+twitPath);
var staticPath = path.resolve(base+'/static/'+twitPath);
if (path.existsSync(publicPath)){
Twit = require(publicPath);
}
else if (path.existsSync(staticPath)){
Twit = require(staticPath);
}
else{
console.log('node_modules not found');
}
meteor deploy should work find after that, sill me for putting my node modules in the server dirs
Just spent a half hour figuring out the "install npm module inside app/public
step and thought I'd save the next person some time. From your app's home directory:
cd public
mkdir node_modules
npm install foo
By default, npm install foo
installs "locally," but if there's no node_modules
folder in your current directory then it moves up the directory tree looking for one. I was ending up with the package installing in $HOME/node_modules/foo
instead of the local project. Fine for localhost
, but not so much for deployment.
(Thanks to npm install locally for solving my root problem.)
This code worked for me with meteor 0.8.x and node_modules being installed in ./public of my app:
var path = Npm.require('path')
var fs = Npm.require('fs')
var base = path.resolve('.')
var isBundle = fs.existsSync(base + '/bundle')
var modulePath = base + (isBundle ? '/bundle/static' : '/../client/app') + '/node_modules/'
var twit = Npm.require(modulePath+'rssparser')
It may also be a good idea to create packages.json file within ./public for easier updates/installs through npm.
Long live Meteor!