我正在使用hubot(特别是hubot-hipchat)并且想要使用https://github.com/scriby/asyncblock节点模块。在hubot中导入/要求节点模块的正确方法是什么?
我cd到我安装hubot(〜/ hubot)的地方并且做了
编辑hubot / packages.json将hubot-hipchat,asyncblock添加到依赖项部分。
"dependencies": {
"hubot-hipchat": ">= 1.0.4",
"hubot": "2.1.4",
"hubot-scripts": ">=2.0.4",
"optparse": "1.0.3",
"asyncblock": "2.0.8"
}
然后我在scripts / test.coffee脚本中执行以下操作:
asyncblock = require('asyncblock')
当我启动hubot时,我收到错误:ERROR错误:无法找到模块'asyncblock'
存在〜/ hubot / node_modules / asyncblock。那么我试着这样做:
require.paths.push('/home/ubuntu/hubot/node_modules')
现在我没有找不到错误,但我得到了一个新错误:
ERROR Error: require.paths is removed. Use node_modules folders, or the NODE_PATH environment variable instead.
我做错了什么?
我只是想在运行hubot的ubuntu服务器上执行系统命令:
asyncblock((flow) ->
exec('node -v', flow.add())
result = flow.wait()
msg.send result
)
编辑: 因此,如果我将NODE_PATH env var设置为以下值,则可以正常工作:
env NODE_PATH="/usr/lib/nodejs:/usr/share/javascript:/home/ubuntu/hubot/node_modules"
为什么不按惯例查看hubot / node_modules?
答案 0 :(得分:0)
您的脚本位于require('asyncblock')
的哪个位置?它在~/hubot
的子目录中吗?如果不是,则其require
函数将不会查看~/hubot/node_modules
。
检查该脚本中module.paths
的值,以查看脚本require
查看的所有非全局路径。
答案 1 :(得分:0)
这里的问题是您正在引用未添加到项目中的npm模块。从hubot文件夹的根目录运行以下代码:
npm install --save asyncblock
这会将asyncblock保存为package.json文件中的依赖项。 因此,如果您部署到heroku或open shift或任何其他云服务,该服务将知道自动下载模块,以便它可供机器人使用。
完成上述任务后
asyncblock= require 'asyncblock'
使用咖啡脚本是正确的
答案 2 :(得分:-1)
这样做要简单得多。我也在使用hubot和hipchat。您所需要做的就是:
cd hubot-dir;
npm install asyncblock
以下是您应该观察的内容:
$ npm install asyncblock
npm http GET https://registry.npmjs.org/asyncblock
npm http 200 https://registry.npmjs.org/asyncblock
npm http GET https://registry.npmjs.org/asyncblock/-/asyncblock-2.0.9.tgz
npm http 200 https://registry.npmjs.org/asyncblock/-/asyncblock-2.0.9.tgz
npm http GET https://registry.npmjs.org/fibers/0.6.7
npm http GET https://github.com/scriby/UglifyJS/tarball/1.2.5-6
npm http 200 https://registry.npmjs.org/fibers/0.6.7
npm http GET https://registry.npmjs.org/fibers/-/fibers-0.6.7.tgz
npm http 200 https://registry.npmjs.org/fibers/-/fibers-0.6.7.tgz
npm http 200 https://github.com/scriby/UglifyJS/tarball/1.2.5-6
npm http GET https://registry.npmjs.org/node-gyp
npm http 200 https://registry.npmjs.org/node-gyp
npm http GET https://registry.npmjs.org/node-gyp/-/node-gyp-0.4.5.tgz
npm http 200 https://registry.npmjs.org/node-gyp/-/node-gyp-0.4.5.tgz
npm http GET https://registry.npmjs.org/ansi
npm http GET https://registry.npmjs.org/glob
npm http GET https://registry.npmjs.org/graceful-fs
npm http GET https://registry.npmjs.org/fstream
npm http GET https://registry.npmjs.org/minimatch
npm http GET https://registry.npmjs.org/rimraf
npm http GET https://registry.npmjs.org/mkdirp
npm http GET https://registry.npmjs.org/nopt
npm http GET https://registry.npmjs.org/semver
npm http GET https://registry.npmjs.org/tar
npm http GET https://registry.npmjs.org/which
npm http GET https://registry.npmjs.org/request
npm http 200 https://registry.npmjs.org/ansi
npm http GET https://registry.npmjs.org/ansi/-/ansi-0.0.4.tgz
npm http 200 https://registry.npmjs.org/graceful-fs
npm http GET https://registry.npmjs.org/graceful-fs/-/graceful-fs-1.1.8.tgz
npm http 304 https://registry.npmjs.org/mkdirp
npm http 200 https://registry.npmjs.org/fstream
npm http GET https://registry.npmjs.org/fstream/-/fstream-0.1.18.tgz
npm http 200 https://registry.npmjs.org/rimraf
npm http GET https://registry.npmjs.org/rimraf/-/rimraf-2.0.2.tgz
npm http 304 https://registry.npmjs.org/nopt
npm http 200 https://registry.npmjs.org/glob
npm http GET https://registry.npmjs.org/glob/-/glob-3.1.9.tgz
npm http 200 https://registry.npmjs.org/semver
npm http GET https://registry.npmjs.org/semver/-/semver-1.0.14.tgz
npm http 304 https://registry.npmjs.org/request
npm http 200 https://registry.npmjs.org/minimatch
npm http GET https://registry.npmjs.org/minimatch/-/minimatch-0.2.5.tgz
npm http 200 https://registry.npmjs.org/which
npm http 200 https://registry.npmjs.org/tar
npm http GET https://registry.npmjs.org/which/-/which-1.0.5.tgz
npm http GET https://registry.npmjs.org/tar/-/tar-0.1.13.tgz
npm http 200 https://registry.npmjs.org/graceful-fs/-/graceful-fs-1.1.8.tgz
npm http 200 https://registry.npmjs.org/ansi/-/ansi-0.0.4.tgz
npm http 200 https://registry.npmjs.org/fstream/-/fstream-0.1.18.tgz
npm http 200 https://registry.npmjs.org/rimraf/-/rimraf-2.0.2.tgz
npm http 200 https://registry.npmjs.org/glob/-/glob-3.1.9.tgz
npm http 200 https://registry.npmjs.org/minimatch/-/minimatch-0.2.5.tgz
npm http 200 https://registry.npmjs.org/semver/-/semver-1.0.14.tgz
npm http 200 https://registry.npmjs.org/which/-/which-1.0.5.tgz
npm http 200 https://registry.npmjs.org/tar/-/tar-0.1.13.tgz
npm http GET https://registry.npmjs.org/abbrev
npm http GET https://registry.npmjs.org/lru-cache
npm http GET https://registry.npmjs.org/inherits
npm http GET https://registry.npmjs.org/inherits
npm http GET https://registry.npmjs.org/inherits
npm http GET https://registry.npmjs.org/block-stream
npm http 304 https://registry.npmjs.org/abbrev
npm http 200 https://registry.npmjs.org/inherits
npm http GET https://registry.npmjs.org/inherits/-/inherits-1.0.0.tgz
npm http 200 https://registry.npmjs.org/inherits
npm http 200 https://registry.npmjs.org/inherits
npm http 200 https://registry.npmjs.org/lru-cache
npm http GET https://registry.npmjs.org/lru-cache/-/lru-cache-1.1.0.tgz
npm http 200 https://registry.npmjs.org/block-stream
npm http GET https://registry.npmjs.org/block-stream/-/block-stream-0.0.5.tgz
npm http 200 https://registry.npmjs.org/inherits/-/inherits-1.0.0.tgz
npm http 200 https://registry.npmjs.org/lru-cache/-/lru-cache-1.1.0.tgz
npm http 200 https://registry.npmjs.org/block-stream/-/block-stream-0.0.5.tgz
fibers@0.6.7 install /tmp/hubot/node_modules/asyncblock/node_modules/fibers
node ./build.js
`linux-ia32` exists; skipping build
asyncblock@2.0.9 ./node_modules/asyncblock
├── uglify-js_scriby@1.2.5-6
└── fibers@0.6.7 (node-gyp@0.4.5)