10

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?

Ugtemlhrshrwzf
  • 1,967
  • 1
  • 14
  • 13
Steeve Cannon
  • 3,682
  • 3
  • 36
  • 49

6 Answers6

6
cd /usr/local/meteor/lib/ && npm install <module>
Botz3000
  • 39,020
  • 8
  • 103
  • 127
  • 8
    Please note, the node modules will not deploy if you are deploying to meteor.com via the deploy function if you use this solution. This solution is good for local development or installing node modules on your own Meteor deployment like AWS or something. If you are deploying to meteor.com use (this solution).[http://stackoverflow.com/questions/10476170/how-can-i-deploy-node-modules-in-a-meteor-app-on-meteor-com] – Steeve Cannon May 31 '12 at 22:43
  • A followup question asking about /usr/local vs /usr/lib has been posted: [Should I install npm modules for Meteor in /usr/lib or /usr/local?](http://stackoverflow.com/q/14304639/16308) – Rahul Jan 13 '13 at 16:50
  • 1
    This did not work in MacOS X 10.9.1 and the latest meteor ( 0.7.0.1 ) .. still can't find the SOAP module regardless of where I install it. Tried the app directory (caused errors), tried the above recommended, and also tried installing the module with -g .. all no avail. – redcap3000 Jan 02 '14 at 21:09
2

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")
Tarang
  • 75,157
  • 39
  • 215
  • 276
1

Meteor takes lib/node_modules from the development bundle and makes a symbolic link or copies it to server/node_modules, which is in the hidden .meteor sub folder under your project.

So, if you cd into the lib directory of the development bundle or into server directory of the .meteor folder (I believe it is in build); you will be able to use the node modules. If you have trouble loading them, you might want to check out this question.

Community
  • 1
  • 1
Tamara Wijsman
  • 12,198
  • 8
  • 53
  • 82
  • Yeah that link is my thread. My worry is that if you put them in /public/node_modules/ you can actually traverse the directory structure from a web browser. Which leads to security issues. When I installed node modules via npm in the /server/ node_modules area they don't get deployed. – Steeve Cannon May 18 '12 at 01:09
1

You have to add bundle folder to the path:

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

Here is my working sample in coffeescript, node_modules are in 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!

kravc
  • 583
  • 5
  • 15
  • Note: since Meteor 0.6.0, `__meteor_bootstrap__.require` has been obsoleted by [Npm.require()](http://stackoverflow.com/a/15351543/1269037) – Dan Dascalescu Dec 22 '13 at 11:39
0

This helped me a lot including a syntax highlighting package! Thanks!

I use a little helper though, as I think this will not be the last npm package I'll 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 like this:

highlight = meteorNpm.require "highlight.js"
Andreas
  • 1,622
  • 14
  • 13
  • 1
    *Note*: since Meteor 0.6.0, `__meteor_bootstrap__.require` has been obsoleted by [Npm.require()](http://stackoverflow.com/a/15351543/1269037) – Dan Dascalescu Dec 22 '13 at 11:40
0

I am using such script which nicely install all node.js dependencies. It behaves similar to official support in Meteor engine branch (it installs dependencies at runtime) but it supports also installing from git repositories and similar goodies.

Mitar
  • 6,756
  • 5
  • 54
  • 86