【问题标题】:how to setup node_path for hubot如何为hubot设置node_path
【发布时间】:2012-05-20 09:16:33
【问题描述】:

我正在使用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 ??

【问题讨论】:

  • 我可以回答您的部分问题:require.paths 在 Node 0.5 中被删除。您遇到的任何使用require.paths.push 的文章或源代码都已过时。我假设你在 Node 0.6.x 上?
  • 节点-v:v0.6.17。是的,我知道它已被弃用/删除。令我困惑的是为什么它没有在 ./node_modules 中找到模块 - 因为它在该目录中找到我的其他模块(如 hubot-hipchat)就好了。

标签: node.js coffeescript hubot


【解决方案1】:

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

npm install --save asyncblock

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

完成以上任务后

asyncblock= require 'asyncblock'

使用咖啡脚本是正确的

【讨论】:

    【解决方案2】:

    这样做要简单得多。我也在使用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)
    

    【讨论】:

      【解决方案3】:

      require('asyncblock') 的脚本在哪里?它在~/hubot 的子目录中吗?如果不是,它的require 函数将不会在~/hubot/node_modules 中查找。

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

      【讨论】:

      • 我的咖啡脚本文件在 ~/hubot/scripts.该脚本还使用来自 hubot-hipchat 的方法(位于 ~/hubot/node_modules 中)。不知道为什么找不到 ~/hubot/node_modules/asyncblock
      • 这里有符号链接吗?也就是说,~/hubot/scripts 是一个真实的目录,~/hubot/scripts/test.coffee 是一个真实的文件吗?同样,请从test.coffee 报告module.paths 的值。
      • ~/hubot/scripts 是一个真正的目录。 ~/hubot/scripts/deploy.coffee(我的脚本)是 deploy.coffee -> /home/ubuntu/git/mln-admin/hubot/scripts/deploy.coffee 的符号链接。但是在这个脚本中,如上所述,我在 ~/hubot/node_modules/hubot-hipchat/ 中使用对象,例如 module.paths 是(有些不存在):/home/ubuntu/git/mln-admin/hubot/scripts/ node_modules,/home/ubuntu/git/mln-admin/hubot/node_modules,/home/ubuntu/git/mln-admin/node_modules,/home/ubuntu/git/node_modules,/home/ubuntu/node_modules,/home/node_modules ,/node_modules
      • 等等,为什么/home/ubuntu/hubot/node_modules 是您添加到NODE_PATH 以使其工作的路径,而/home/ubuntu/git/mln-admin/hubot/node_modulesmodule.paths 中的路径? /home/ubuntu/git/mln-admin/hubot/node_modules 是否包含带有package.jsonlib/asyncblock.jsasyncblock 子目录?
      • 因为 /home/ubuntu/hubot/node_modules 是安装 hubot 以及 hubot-hipchat 和 asyncblock 的位置。 〜上面的问题和cmets是'/home/ubuntu'。 /home/ubuntu/hubot/scripts/deploy.coffee 是我的脚本。是 /home/ubuntu/git/mln-admin/hubot/node_modules/lib/asyncblock.js 存在,可以在设置 NODE_PATH 环境变量时使用。
      猜你喜欢
      • 2017-02-18
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 2021-02-13
      相关资源
      最近更新 更多