I have an application that uses the node twit module that is available via
npm install twit
I deployed the node module locally from .m
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!
You
base = base + "/bundle"
to get this to work.
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');
});
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.)
Changed:
var modulePath = base + (isBundle ? '/bundle/static' : '/../client/app') + '/node_modules/'
to:
var modulePath = base + (isBundle ? '/bundle/static' : '/../web.browser/app') + '/node_modules/'
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