NPM全局安装“找不到模块" [英] NPM global install "cannot find module"

查看:58
本文介绍了NPM全局安装“找不到模块"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个模块,我刚刚发布到 npm (https://npmjs.org/package/wisp)

I wrote a module which I published to npm a moment ago (https://npmjs.org/package/wisp)

所以它从命令行安装得很好:

So it installs fine from the command line:

$ npm i -g wisp

但是,当我从命令行运行它时,我不断收到未安装 optimist 的错误:

However, when I run it from the command line, I keep getting an error that optimist isn't installed:

$ wisp 
Error: Cannot find module 'optimist'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (/usr/local/lib/node_modules/wisp/wisp:12:10)
    at Object.<anonymous> (/usr/local/lib/node_modules/wisp/wisp:96:4)
    at Module._compile (module.js:449:26)
    at Object.exports.run (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:68:25)
    at compileScript (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:135:29)
    at fs.stat.notSources.(anonymous function) (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:110:18)

但是,我在 package.json 中指定了一个依赖项:

However, I have specified in package.json as a dependancy:

{
  "name": "wisp",
  "author": "Brendan Scarvell <bscarvell@gmail.com>",
  "version": "0.1.0",
  "description": "Global nodejs file server",
  "dependencies": {
    "optimist": "~0.3.4"
  },
  "repository": "git://github.com/tehlulz/wisp",
  "bin": {
    "wisp" : "./wisp"
  }
}

有谁知道怎么做才能让它运行?我知道这与将可执行文件添加到 bin 并且该目录中的 node_modules 为空的 bin 部分有关.不知道如何解决这个问题.

Does anyone know what to do to get this running? I know its to do with the bin part adding the executable to bin and the node_modules in that directory being empty. No idea how to resolve this.

推荐答案

对于遇到此问题的其他人,我遇到了这个问题,因为我的 npm 安装到了一个不在我的 上的位置NODE_PATH.

For anyone else running into this, I had this problem due to my npm installing into a location that's not on my NODE_PATH.

[root@uberneek ~]# which npm
/opt/bin/npm
[root@uberneek ~]# which node
/opt/bin/node
[root@uberneek ~]# echo $NODE_PATH

我的 NODE_PATH 是空的,运行 npm install --global --verbose promise-io 显示它正在安装到 /opt/lib/node_modules/promised-io:

My NODE_PATH was empty, and running npm install --global --verbose promised-io showed that it was installing into /opt/lib/node_modules/promised-io:

[root@uberneek ~]# npm install --global --verbose promised-io
npm info it worked if it ends with ok
npm verb cli [ '/opt/bin/node',
npm verb cli   '/opt/bin/npm',
npm verb cli   'install',
npm verb cli   '--global',
npm verb cli   '--verbose',
npm verb cli   'promised-io' ]
npm info using npm@1.1.45
npm info using node@v0.8.4
[cut]
npm info build /opt/lib/node_modules/promised-io
npm verb from cache /opt/lib/node_modules/promised-io/package.json
npm verb linkStuff [ true, '/opt/lib/node_modules', true, '/opt/lib/node_modules' ]
[cut]

我的脚本在 require('promised-io/promise') 上失败:

My script fails on require('promised-io/promise'):

[neek@uberneek project]$ node buildscripts/stringsmerge.js 

module.js:340
    throw err;
          ^
Error: Cannot find module 'promised-io/promise'
    at Function.Module._resolveFilename (module.js:338:15)

我可能使用 configure --prefix=/opt 从源代码安装了 node 和 npm.我不知道为什么这让他们无法找到已安装的模块.现在的解决方法是将 NODE_PATH 指向正​​确的目录:

I probably installed node and npm from source using configure --prefix=/opt. I've no idea why this has made them incapable of finding installed modules. The fix for now is to point NODE_PATH at the right directory:

export NODE_PATH=/opt/lib/node_modules

我的 require('promised-io/promise') 现在成功了.

这篇关于NPM全局安装“找不到模块"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆