如何在meteor.com上的Meteor应用程序中部署节点模块?

Ste*_*non 19 meteor

我有一个应用程序,它使用可通过的节点twit模块

npm install twit
Run Code Online (Sandbox Code Playgroud)

我从.meteor/local/build/server /本地部署了节点模块

因此,它在.meteor/local/build/server/node_modules/twit中可见

我尝试在项目根目录下安装它,但项目没有找到模块.这使我得到了上述有效的解决方案.

我的应用程序现在运行正常.我可以运行并做所有事情,并可以从我的Meteor服务器端或客户端与Twitter进行交互,具体取决于我想要做什么.没有崩溃.

当我通过命令部署到meteor.com时

meteor deploy [appname] --password
Run Code Online (Sandbox Code Playgroud)

应用程序成功部署.

当我尝试从浏览器访问(anonistream.meteor.com上的应用程序)[anonistream.meteor.com]时,它失败并且日志包含此错误.

[Mon May 07 2012 01:59:53 GMT+0000 (UTC)] WARNING
node.js:201
   throw e; // process.nextTick error, or 'error' event on first tick
         ^
Error: Cannot find module 'twit'
at Function._resolveFilename (module.js:332:11)
at Function._load (module.js:279:25)
at Module.require (module.js:354:17)
at require (module.js:370:17)
at app/server/server.js:2:12
at /meteor/containers/84162a7c-24e8-bf26-6fd8-e4ec13b2a935/bundle/server/server.js:111:21
at Array.forEach (native)
at Function.<anonymous>
 (/meteor/containers/84162a7c-24e8-bf26-6fd8-     e4ec13b2a935/bundle/server/underscore.js:76:11)
at /meteor/containers/84162a7c-24e8-bf26-6fd8-e4ec13b2a935/bundle/server/server.js:97:7
[Mon May 07 2012 01:59:53 GMT+0000 (UTC)] INFO STATUS running -> waiting
[Mon May 07 2012 01:59:53 GMT+0000 (UTC)] ERROR Application crashed with code: 1
[Mon May 07 2012 02:29:55 GMT+0000 (UTC)] INFO HIT / 24.94.158.145
[Mon May 07 2012 02:29:59 GMT+0000 (UTC)] INFO HIT /favicon.ico 24.94.158.145
[Mon May 07 2012 02:30:46 GMT+0000 (UTC)] INFO HIT / 24.94.158.145
[Mon May 07 2012 02:30:50 GMT+0000 (UTC)] INFO HIT /favicon.ico 24.94.158.145
Run Code Online (Sandbox Code Playgroud)

有没有人对如何实现这一点有任何建议?

小智 14

从Meteor 6.0开始,现在我们需要使用Npm.require().此外,我们需要将模块声明为全局变量,因为Meteor现在具有文件级范围.

  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
Run Code Online (Sandbox Code Playgroud)


小智 8

最后,我这样写了.它适用于本地和流星服务器.伊恩:D

在"app/public"中安装npm模块:

    app/public# npm install MODULE_NAME

在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');
});
Run Code Online (Sandbox Code Playgroud)

  • 注意:由于Meteor 0.6.0,`__ meteor_bootstrap __.require`已被[Npm.require()](http://stackoverflow.com/a/15351543/1269037)废弃 (3认同)

Ste*_*non 7

在流星号上找到JonathanKingston的答案.提到流星项目

将节点模块放在项目公共目录中.

使用这样的代码来确保它加载.

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');
}
Run Code Online (Sandbox Code Playgroud)

meteor deploy应该在那之后找到工作,让我把我的节点模块放在服务器目录中