Alex Rivera | Logout

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

Asked 2012-05-14T17:08:41.360
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?

Edit
Report

1 Answer

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"
answered 2012-08-31T11:38:35.327

Your Answer