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

如何在Meteor.com上的Meteor应用程序中部署节点模块?,meteor,Meteor,我有一个使用node twit模块的应用程序,该模块可通过 npm install twit 我从本地部署了节点模块 .meteor/local/build/server/ 因此,它在 .meteor/local/build/server/node_modules/twit 我尝试在项目根目录下安装它,但项目没有找到模块。这让我找到了上述有效的解决方案 我的应用程序现在在本地运行良好。我可以运行和做任何事情,可以从Meteor服务器端或客户端与Twitter交互,具体取决于我想做什么。没有撞车

我有一个使用node twit模块的应用程序,该模块可通过

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

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

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

我的应用程序现在在本地运行良好。我可以运行和做任何事情,可以从Meteor服务器端或客户端与Twitter交互,具体取决于我想做什么。没有撞车

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

meteor deploy [appname] --password
应用程序部署成功

当我试图从浏览器访问(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
[Mon May 07 2012 01:59:53 GMT+0000(UTC)]警告
node.js:201
抛出e;//process.nextTick错误,或第一次勾选时的“error”事件
^
错误:找不到模块“twit”
at Function.\u解析文件名(module.js:332:11)
at功能。加载(模块js:279:25)
at Module.require(Module.js:354:17)
根据需要(模块js:370:17)
在app/server/server.js:2:12
at/meteor/containers/84162a7c-24e8-bf26-6fd8-e4ec13b2a935/bundle/server/server.js:111:21
at Array.forEach(本机)
在功能上。
(/meteor/containers/84162a7c-24e8-bf26-6fd8-e4ec13b2a935/bundle/server/underline.js:76:11)
at/meteor/containers/84162a7c-24e8-bf26-6fd8-e4ec13b2a935/bundle/server/server.js:97:7
[2012年5月7日星期一01:59:53 GMT+0000(UTC)]信息状态运行->等待
[2012年5月7日星期一01:59:53 GMT+0000(UTC)]错误应用程序崩溃,代码为:1
[2012年5月7日星期一02:29:55 GMT+0000(UTC)]信息点击/24.94.158.145
[2012年5月7日星期一02:29:59 GMT+0000(UTC)]信息点击/favicon.ico 24.94.158.145
[2012年5月7日星期一02:30:46 GMT+0000(UTC)]信息点击/24.94.158.145
[2012年5月7日星期一02:30:50 GMT+0000(UTC)]信息点击/favicon.ico 24.94.158.145

有人对如何实现这一点有什么建议吗?

答案来自JonathanKingston在meteor irc上找到的。提及

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

使用类似这样的代码来确保它已加载

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');
}
meteor deploy应该可以在这之后找到,谢谢你把我的节点模块放到服务器目录中

base = base + "/bundle"

最后,我这样写。 它在本地和流星服务器上都能工作。thx伊恩:D

在“应用程序/公共”中安装npm模块:


从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

刚刚花了半个小时来计算“在
app/public
中安装npm模块”步骤,我想我会为下一个人节省一些时间。从你的应用程序的主目录:

cd public
mkdir node_modules
npm install foo
默认情况下,
npm install foo
installs“本地,“但是如果当前目录中没有
node\u modules
文件夹,则它会向上移动目录树以查找其中一个。我最终在
$HOME/node\u modules/foo
中安装了包,而不是本地项目。适用于
localhost
,但不适用于部署


(感谢您解决了我的根本问题。)

我的应用程序的./public中安装了meteor 0.8.x和node_模块,这段代码对我有效:

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')
最好在./public中创建packages.json文件,以便通过npm更轻松地进行更新/安装

流星万岁

改变:

var modulePath = base + (isBundle ? '/bundle/static' : '/../client/app') + '/node_modules/'
致:


正在检查…但是twit是否包含在您的
package.json
文件中?我不知道我必须构建meteor包才能在部署中包含twit。我必须调查一下,看看情况是否如此。我假设node_模块中的任何内容都直接推送到部署上。明天我要翻阅资料,看看能找到什么!除非其他人发布的答案是!老实说,我也不确定…期待听到其他人的意见。就我个人而言,我现在有一个包.json用于我所有的meteor项目。但是,我现在只部署到Heroku(用于具有节点依赖关系的高级应用程序)。方法如下:您省略了允许您在Meteor上
require
的代码行:
var require=\uuuuuuuuMeteor\u bootstrap\uuuuu.require。这让我有点不知所措,我知道很多人在这里从IRC.path.existsSync()被指出来。existsSync()现在是fs.existsSync()注:自从Meteor 0.6.0以来,
\uMeteor\u bootstrap\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu?我做不到。每次我对应用程序文件夹进行更改时,meteor重新启动时,它就会被覆盖。好的,我让它工作了。不得不添加这一行:
var fs=require('fs')和修改:
var isBundle=path.existsSync(base+'/bundle')
into
var isBundle=fs.existsSync(base+'/bundle')注意:自Meteor 0.6.0以来,
Meteor\u引导程序要求已被淘汰,如果您想使用外部npm软件包。使用Npm智能包()。这就像一个符咒:)这是2年前的做法。请参阅其他答案。您能否澄清在meteor项目中将Npm.require()语句放在何处?因此,在meteor中使用Npm包时,您必须这样做,而不仅仅是使用meteor.require?如果npm模块位于public/node_modules文件夹中,那么npm模块在客户端和服务器上都可用吗?这种使用npm模块的方法会导致问题吗?我只是不知道将npm模块集成到我的项目中以在服务器上使用的最佳方式是什么。@supertrue:我的评论有点晚了,但这是对spectrum答案的某种回复(目前已被接受的答案)。所以它可以在app/server/server.js中。Followup:不久之后,我发现meteorite包
npm
可以做这种事情,它就像一个符咒。
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')
var modulePath = base + (isBundle ? '/bundle/static' : '/../client/app') + '/node_modules/'
var modulePath = base + (isBundle ? '/bundle/static' : '/../web.browser/app') + '/node_modules/'