3

我正在使用 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时,我收到错误:错误错误:找不到模块'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 NODE_PATH="/usr/lib/nodejs:/usr/share/javascript:/home/ubuntu/hubot/node_modules"

为什么不按惯例查看 hubot/node_modules ?

4

3 回答 3

0

你的脚本在哪里require('asyncblock')?它在 的子目录中~/hubot吗?如果没有,它的require功能将不会在~/hubot/node_modules.

检查该脚本的值module.paths以查看该脚本查看的所有非全局路径require

于 2012-05-12T15:03:30.553 回答
0

这里的问题是您引用了一个未添加到项目中的 npm 模块。从 hubot 文件夹的根目录运行以下代码:

npm install --save asyncblock

这会将 asyncblock 保存为 package.json 文件中的依赖项。因此,如果您部署到 heroku 或 open shift 或任何其他云服务,该服务将知道自动下载该模块,以便您的机器人可以使用它。

完成上述任务后

asyncblock= require 'asyncblock'

使用咖啡脚本是正确的

于 2015-11-19T01:02:41.557 回答
-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)
于 2012-06-12T01:23:33.663 回答