node.js NPM 全局安装“找不到模块”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12594541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:21:29  来源:igfitidea点击:

NPM global install "cannot find module"

node.jsnpm

提问by Menztrual

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

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

So it installs fine from the command line:

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

$ npm i -g wisp

$ npm i -g wisp

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

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

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

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

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

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

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.

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

回答by Neek

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

对于遇到此问题的其他任何人,由于我npm安装到不在我的NODE_PATH.

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

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

我的 NODE_PATH 是空的,运行npm install --global --verbose promised-io表明它正在安装到/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 [email protected]
npm info using [email protected]
[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]

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

我的脚本失败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)

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:

我可能使用configure --prefix=/opt. 我不知道为什么这让他们无法找到已安装的模块。现在的修复是将 NODE_PATH 指向正​​确的目录:

export NODE_PATH=/opt/lib/node_modules

My require('promised-io/promise')now succeeds.

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

回答by Cando Zhou

add this to beginning of prog(mac):

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

module.paths.push('/usr/local/lib/node_modules');

module.paths.push('/usr/local/lib/node_modules');

回答by randomness

By default node does not look inside the /usr/local/lib/node_module for loading global modules. Refer the module loading explained in http://nodejs.org/api/modules.html#modules_loading_from_the_global_folders

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

So either you have to 1)add the /usr/local/lib/node_module to NODE_PATH and export it or 2)copy the installed node modules to /usr/local/lib/node . (As explained in the link for loading module node will search in this path and will work)

因此,您必须 1) 将 /usr/local/lib/node_module 添加到 NODE_PATH 并将其导出或 2) 将安装的节点模块复制到 /usr/local/lib/node 。(如加载模块链接中所述,节点将在此路径中搜索并起作用)

回答by Will

For some (like me) that nothing else worked, try this:

对于一些(像我一样)没有其他工作的人,试试这个:

brew cleanup
brew link node
brew uninstall node
brew install node

Hope it helps someone :)

希望它可以帮助某人:)

回答by user2921139

I got the "optimist" module error and I just did "npm install" to resolve it. went past that error.

我收到了“乐观主义者”模块错误,我只是做了“npm install”来解决它。过了那个错误。

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

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

回答by xameeramir

The following generic fix would for any module. For example with request-promise.

Replace

以下通用修复适用于任何模块。例如与request-promise.

代替

npm install request-promise --global

With

npm install request-promise --cli

worked (source) and also for globalsand inherits

工作(来源),也为globalsinherits

Also, try setting the environment variable

另外,尝试设置环境变量

NODE_PATH=%AppData%\npm\node_modules

回答by user1959076

$ vim /etc/profile.d/nodejs.sh

$ vim /etc/profile.d/nodejs.sh

NODE_PATH=/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript
export NODE_PATH="$NODE_PATH"

回答by Shivansh Rajpoot

For Mac User's It's Best use the manual installation:

对于 Mac 用户,最好使用手动安装:

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.

  1. Back-up your computer before you start.

  2. Make a directory for global installations:

    mkdir ~/.npm-global

  3. Configure npm to use the new directory path:

    npm config set prefix '~/.npm-global'

  4. Open or create a ~/.profile file and add this line:

    export PATH=~/.npm-global/bin:$PATH

  5. Back on the command line, update your system variables:

    source ~/.profile

  6. 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

为了最大限度地减少权限错误的可能性,您可以将 npm 配置为使用不同的目录。在本例中,它将是您的主文件夹中的一个隐藏目录。

  1. 在开始之前备份您的计算机。

  2. 为全局安装创建一个目录:

    mkdir ~/.npm-global

  3. 配置 npm 以使用新的目录路径:

    npm 配置设置前缀'~/.npm-global'

  4. 打开或创建一个 ~/.profile 文件并添加以下行:

    导出路径=~/.npm-global/bin:$PATH

  5. 回到命令行,更新您的系统变量:

    源 ~/.profile

  6. 测试:不使用 sudo 全局下载包。

    npm install -g jshint

您可以使用相应的 ENV 变量代替步骤 2-4(例如,如果您不想修改 ~/.profile):

NPM_CONFIG_PREFIX=~/.npm-global

Reference : https://docs.npmjs.com/getting-started/fixing-npm-permissions

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

回答by P M

For Windows, from Nodejs cannot find installed module on Windows?what worked for me is running npm link as in

对于 Windows,从Nodejs 找不到 Windows 上已安装的模块?对我有用的是运行 npm link

npm link wisp

回答by GT.

In my case both nodeand npmwere in same path (/usr/bin). The NODE_PATHwas empty, so the npmplaced the global modules into /usr/lib/node_moduleswhere require(...)successfully find them. The only exception was the npmmodule, which came with the nodejs package. Since I'm using 64 bit system, it was placed into /usr/lib64/node_modules. This is not where require(...) searches in case of empty NODE_PATHand node started from /usr/bin. So I had two options:

在我的情况都nodenpm都在同一路径(/usr/bin)。该NODE_PATH是空的,所以npm放在全局模块到/usr/lib/node_modules那里require(...)顺利地找到它们。唯一的例外是npmnodejs 包附带的模块。由于我使用的是 64 位系统,所以它被放入/usr/lib64/node_modules. 这不是 require(...) 在为空NODE_PATH且节点从/usr/bin. 所以我有两个选择:

  • link /usr/lib64/node_modules/npmto /usr/lib/node_modules/npm
  • move modules from /usr/lib/node_modules/*to /usr/lib64/node_modules/and set NODE_PATH=/usr/lib64/node_modules
  • 链接/usr/lib64/node_modules/npm/usr/lib/node_modules/npm
  • /usr/lib/node_modules/*to/usr/lib64/node_modules/和 set移动模块NODE_PATH=/usr/lib64/node_modules

Both worked. I'm using OpenSUSE 42.1 and the nodejs package from updates repository. Version is 4.4.5.

两者都有效。我正在使用 OpenSUSE 42.1 和更新存储库中的 nodejs 包。版本是 4.4.5。