Javascript 使用 npm 命令时找不到模块“npmlog”错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34010476/
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
Cannot find module 'npmlog' error message when using npm command
提问by Parker Lewis
Today is my first day of my first job and after setting up my development environment by updating node and npm on my Mac (OSX) I seem to have broken something. Every time I try to use npm in my commandline (npm init, npm install, etc.) I get this error message:
今天是我第一份工作的第一天,通过在 Mac (OSX) 上更新 node 和 npm 来设置我的开发环境后,我似乎破坏了一些东西。每次我尝试在命令行(npm init、npm install 等)中使用 npm 时,我都会收到以下错误消息:
module.js:339
throw err;
Error: Cannot find module 'npmlog'
at Function.Module._resolveFilename (module.js:337:15)
at Function.Module._load (module.js:287:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at /usr/local/lib/node_modules/npm/bin/npm-cli.js:20:13
at Object. (/usr/local/lib/node_modules/npm/bin/npm->cli.js:76:3)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
模块.js:339
抛出错误;
错误:找不到模块“npmlog”
在 Function.Module._resolveFilename (module.js:337:15)
在 Function.Module._load (module.js:287:25)
在 Module.require (module.js:366:17)
在要求 (module.js:385:17)
在 /usr/local/lib/node_modules/npm/bin/npm-cli.js:20:13
在对象。(/usr/local/lib/node_modules/npm/bin/npm->cli.js:76:3)
在 Module._compile (module.js:425:26)
在 Object.Module._extensions..js (module.js:432:10)
在 Module.load (module.js:356:32)
在 Function.Module._load (module.js:311:12)
There isn't much of an error message to go off and the stack trace isn't helping me all that much either. Here's what I have tried:
没有太多的错误消息要发出,堆栈跟踪也没有帮助我那么多。这是我尝试过的:
- Searched around Stackoverflow and though there are similar problems, people have been able to use
npm <something>
to solve their problem, which I cannot do. - Uninstalling and reinstalling npm - Didn't work because I can't use npm command at all so things like
sudo npm uninstall npm -g
don't work. - Removed
npmlog
directory from.npm
directory and then cloned the github repo directly to the directory again.
- 在 Stackoverflow 周围搜索,虽然有类似的问题,但人们已经能够
npm <something>
用来解决他们的问题,而我做不到。 - 卸载并重新安装 npm - 没有用,因为我根本不能使用 npm 命令,所以像这样的东西
sudo npm uninstall npm -g
不起作用。 npmlog
从.npm
目录中删除目录,然后再次将 github repo 直接克隆到目录。
Hopefully one of you have run into this before or have an idea of how to approach it. Any help is appreciated.
希望你们中的一个人以前遇到过这个问题,或者知道如何处理它。任何帮助表示赞赏。
回答by zangw
Here is one good link Fixing npm On Mac OS X for Homebrew Users
.
这是一个很好的链接Fixing npm On Mac OS X for Homebrew Users
。
with following commands when try to upgrade node.js
under mac
node.js
在mac下尝试升级时使用以下命令
rm -rf /usr/local/lib/node_modules brew uninstall node brew install node --without-npm echo prefix=~/.npm-packages >> ~/.npmrc curl -L https://www.npmjs.com/install.sh | sudo sh
rm -rf /usr/local/lib/node_modules brew uninstall node brew install node --without-npm echo prefix=~/.npm-packages >> ~/.npmrc curl -L https://www.npmjs.com/install.sh | sudo sh
回答by drosenblatt
Here are the steps that worked for me:
以下是对我有用的步骤:
$ sudo rm -rf /usr/local/bin/npm
$ sudo rm -rf /usr/local/lib/node_modules/npm
$ brew uninstall node
$ brew install node
回答by nickleefly
Try curl -0 -L http://npmjs.org/install.sh| sudo sh
to follow redirects. Also notice I added sudo before the sudo sh command depending on your users permissions.
尝试遵循重定向。另请注意,我根据您的用户权限在 sudo sh 命令之前添加了 sudo。curl -0 -L http://npmjs.org/install.sh| sudo sh
回答by Kevin Teljeur
I've just encountered this issue on a Mac, and on closer inspection it seems to me that the problem is that inside the Node application directory in certain circumstances the link in the node/bin
directory to npm
in lib/node_modules/npm/bin
is replaced by the file itself and that is why it fails (and why all the various script methods fix it, because they correctly retain the file link).
我刚刚在 Mac 上遇到了这个问题,经过仔细检查,在我看来问题是在 Node 应用程序目录中,在某些情况下,node/bin
目录中到npm
in的链接lib/node_modules/npm/bin
被文件本身替换,这就是为什么它失败(以及为什么所有各种脚本方法都会修复它,因为它们正确地保留了文件链接)。
If I copy the contents from node-v6.9.4-darwin-x64
with cp
then I get this problem, but if I copy the files using the Finder, then I don't. So, you can fix it by manually recreating the link with ln
if you don't want to use the Finder (alternatively, mv
or rsync -a
could work too; I haven't tested them).
如果我从node-v6.9.4-darwin-x64
with复制内容,cp
则会出现此问题,但如果我使用 Finder 复制文件,则不会。因此,ln
如果您不想使用 Finder ,您可以通过手动重新创建链接来修复它(或者,mv
或者rsync -a
也可以工作;我还没有测试过它们)。
Hope this helps someone coming here for this problem.
希望这有助于有人来这里解决这个问题。
Edit: I've just accidentally found the reason for the problem on Macs (for my particular scenario, at any rate): I've always used cp -r
to copy directories. This is because I am old, and don't learn new things any more. The correct way is cp -R
, which correctly copies everything, as it should.
编辑:我只是偶然发现了 Mac 上问题的原因(无论如何,对于我的特定情况):我一直习惯于cp -r
复制目录。这是因为我老了,不再学新东西了。正确的方法是cp -R
,它应该正确地复制所有内容。
回答by icc97
I hit this on Fedora 27 after I ran sudo npm install npm -g
.
跑完后,我在 Fedora 27 上遇到了这个问题sudo npm install npm -g
。
The problem was that I had initially installed Node.js via sudo dnf install nodejs
which installs npm into /usr/local/bin
and /usr/local/lib
. However running sudo npm install
installs a separate version in /usr/bin/
问题是我最初安装了 Node.js,通过sudo dnf install nodejs
它将 npm 安装到/usr/local/bin
和/usr/local/lib
. 但是运行sudo npm install
会安装一个单独的版本/usr/bin/
Running which npm
shows up that the extra npm is installed. Then the following cleaned everything for me:
运行which npm
显示安装了额外的 npm。然后以下内容为我清理了一切:
$ sudo rm /usr/bin/npm
$ sudo rm -rf /usr/lib/node_modules/npm
$ dnf remove -y nodejs
$ dnf install -y nodejs
回答by Parker Lewis
I didn't necessarily "fix" the problem as much as just start over. I used this post to do so: How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)
我不一定像重新开始那样“解决”问题。我使用这篇文章这样做:如何完全卸载 Node.js,并从头开始重新安装 (Mac OS X)
If anyone else knows how to actually fix the problem or knows what I did to cause it I would still be very interested.
如果其他人知道如何实际解决问题或知道我做了什么导致它,我仍然会非常感兴趣。
回答by Zander Zhang
in my condition, I just
brew uninstall node
and download from the official site and worked for me
在我的情况下,我只是
brew uninstall node
从官方网站下载并为我工作
回答by Ken
I encountered this same error. This was on a server not connected to the internet. I had seen a thread on Github that mentioned sometimes symbolic links can be removed while performing copies or other file operations. So I did a "find" on the node directory for symbolic links of the problematic server and none where found. Doing the same on a working server found symbolic link files.
我遇到了同样的错误。这是在未连接到互联网的服务器上。我在 Github 上看到一个线程,提到有时在执行复制或其他文件操作时可以删除符号链接。因此,我在节点目录上对有问题的服务器的符号链接进行了“查找”,但没有找到。在工作服务器上做同样的发现符号链接文件。
I went to NODE_HOME/binand did an ls -l
我去了NODE_HOME/bin并做了一个ls -l
Sure enough I had a file for npm. Performed the same steps on a working server and the npm file was a symbolic link. To fix the problematic server, I performed the following:
果然我有一个 npm 文件。在工作服务器上执行相同的步骤,npm 文件是一个符号链接。为了修复有问题的服务器,我执行了以下操作:
rm npm
rm npm
ln -s ../lib/node_modules/npm/bin/npm-cli.js npm
ln -s ../lib/node_modules/npm/bin/npm-cli.js npm
回答by Yusuf Kapasi
Got the same error while installing protractor in REDHAT linux updated the
在 REDHAT linux 中安装量角器时遇到同样的错误更新了
npm
新产品经理
file at
文件在
/node-v6.10.2-linux-x64/bin/npm
/node-v6.10.2-linux-x64/bin/npm
for the module path as below:
对于模块路径如下:
Code Before
之前的代码
var log = require('npmlog')
Code After
代码之后
var log = require('/node-v6.10.2-linux-x64/lib/node_modules/npm/node_modules/npmlog')
回答by 0xa6a
For me, I ran brew uninstall node
and then download the latest version from nodejs.organd it worked.
对我来说,我跑了brew uninstall node
然后从nodejs.org下载了最新版本,它工作正常。