最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Where do we put node modules we install by npm in a Meteor project? - Stack Overflow

programmeradmin1浏览0评论

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.

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

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?

Share Improve this question edited May 21, 2012 at 22:49 Ugtemlhrshrwzf 2,1051 gold badge15 silver badges13 bronze badges asked May 14, 2012 at 17:08 Steeve CannonSteeve Cannon 3,6823 gold badges37 silver badges49 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 6
cd /usr/local/meteor/lib/ && npm install <module>

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")

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.

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!

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"

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.

发布评论

评论列表(0)

  1. 暂无评论