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

所以它可以从命令行安装:

I -g wisp

然而,当我从命令行运行它时,我一直得到一个错误,乐观主义者没有安装:

$ 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)

但是,我已经在包中指定了。Json作为依赖项:

{
  "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部分添加可执行文件到bin和该目录中的node_modules为空有关。不知道怎么解决。


当前回答

在根目录下运行Ubuntu的一台测试服务器上遇到了同样的问题。然后使用useradd -m myuser创建一个新用户,并以myuser的身份安装所有内容(nvm, node, packages)。现在工作得很好。

其他回答

下面的通用修复将适用于任何模块。例如请求-承诺。 取代

npm install request-promise --global

npm install request-promise --cli

工作(源),也适用于全局变量和继承

另外,尝试设置环境变量

NODE_PATH=%AppData%\npm\node_modules

对于Mac用户的It's Best使用手动安装:

To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, it will be a hidden directory on your home folder. Back-up your computer before you start. Make a directory for global installations: mkdir ~/.npm-global Configure npm to use the new directory path: npm config set prefix '~/.npm-global' Open or create a ~/.profile file and add this line: export PATH=~/.npm-global/bin:$PATH Back on the command line, update your system variables: source ~/.profile Test: Download a package globally without using sudo. npm install -g jshint Instead of steps 2-4, you can use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile): NPM_CONFIG_PREFIX=~/.npm-global

参考资料:https://docs.npmjs.com/getting-started/fixing-npm-permissions

在我的例子中,node和npm都在同一个路径(/usr/bin)。NODE_PATH是空的,所以npm将全局模块放在/usr/lib/node_modules中,require(…)成功地找到了它们。 唯一的例外是npm模块,它随nodejs包一起提供。因为我使用的是64位系统,所以它被放置在/usr/lib64/node_modules中。这不是require(…)在空NODE_PATH和从/usr/bin开始的节点的情况下搜索的地方。所以我有两个选择:

链接/usr/lib64/node_modules/npm到/usr/lib/node_modules/npm 将模块从/usr/lib/node_modules/*移动到/usr/lib64/node_modules/,并设置NODE_PATH=/usr/lib64/node_modules

这两个工作。我使用OpenSUSE 42.1和更新存储库中的nodejs包。版本为4.4.5。

默认情况下,node不会在/usr/local/lib/node_module中加载全局模块。 参考http://nodejs.org/api/modules.html#modules_loading_from_the_global_folders中解释的模块加载

所以要么你必须 1)将“/usr/local/lib/node_module”添加到“NODE_PATH”并导出 或 2)将已安装的节点模块复制到/usr/local/lib/node。 (如加载模块节点的链接中所解释的那样,将在此路径中搜索并工作)

遇到同样的问题,并通过在我的zshrc中添加下面的行来解决它。 基于您的shell,您可以尝试在您的主位置中添加您的rc文件,用于bash和zsh - bashrc/zshrc文件。

export NODE_PATH="/usr/local/lib/node_modules"

如果需要直接添加到zshrc文件中,使用此命令

echo 'export NODE_PATH="/usr/local/lib/node_modules"' >> ~/.zshrc