我写了一个模块,不久前发布到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为空有关。不知道怎么解决。


当前回答

将此添加到prog(mac)的开头:

module.paths.push(“/ usr /地方/ lib / node_modules”);

其他回答

我遇到了与OP相同的错误,但通过挖掘日志,我可以看到sh: node:命令没有找到。

原来/usr/bin/node程序(symlink)不再安装apt install nodejs。一旦符号链接/usr/bin/node' tonodejs,npm install -g @angular/cli '成功。

在debian上安装它的正确方法是安装nodejs-legacy。

我得到了“乐观”模块错误,我只是做了“npm安装”来解决它。跳过了这个错误。

https://github.com/mbloch/mapshaper/issues/12

对于一些(像我一样)其他方法都不起作用的人来说,试试这个:

brew cleanup
brew link node
brew uninstall node
brew install node

希望它能帮助到别人:)

对于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

对于其他遇到这个问题的人,我有这个问题,因为我的npm安装到一个不在我的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 promises -io显示它正在安装到/opt/lib/node_modules/ promises -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(' promises -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指向正确的目录:

export NODE_PATH=/opt/lib/node_modules

我的require(' promises -io/promise')现在成功了。