Javascript Npm 安装找不到模块“semver”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33870520/
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-08-23 15:35:01  来源:igfitidea点击:

Npm install cannot find module 'semver'

javascriptnode.jsmean-stack

提问by user2447562

I can't use npm installusing the command prompt in NodeJS. I'm getting these errors when running npm install:

我无法npm install在 NodeJS 中使用命令提示符。运行时出现这些错误npm install

module.js:339
    throw err;
    ^
Error: Cannot find module 'semver'
    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 Object.<anonymous> (C:\Users\admin\AppData\Roaming\npm\node_modules\npm\l
ib\config\defaults.js:6:14)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Module.require (module.js:366:17)

回答by artiebits

I had the same error. npm uninstall npm -g, rm -rf node_modulesdidn't help me, because when I tried I was getting Error: Cannot find module 'semver'. But I solve my problem with these steps (this will delete other global modules you may be using):

我有同样的错误。npm uninstall npm -grm -rf node_modules没有帮助我,因为当我尝试时,我得到了Error: Cannot find module 'semver'. 但是我通过这些步骤解决了我的问题(这将删除您可能正在使用的其他全局模块):

  • sudo rm -rf /usr/local/lib/node_modules
  • sudo rm -rf ~/.npm
  • brew uninstall --force node
  • brew install node
  • sudo rm -rf /usr/local/lib/node_modules
  • sudo rm -rf ~/.npm
  • brew uninstall --force node
  • brew install node

Hope this will help those who are getting a similar problem.

希望这能帮助那些遇到类似问题的人。

回答by Ashoor

I'm facing the same issue here.

我在这里面临同样的问题。

If this occurs right after you run brew install yarntry running yarn global add npmand voilà - fixed!

如果在您运行后立即发生这种情况,请brew install yarn尝试运行yarn global add npm并瞧 - 已修复!

回答by Gangadhar

On MS Windows, the solution is to remove %APPDATA%\npmand reinstall node

在 MS Windows 上,解决方案是删除%APPDATA%\npm并重新安装node

回答by raffjones

I had this too, after running brew install yarnyesterday. At least, everything was fine up until then.

brew install yarn昨天跑步后我也有这个。至少,在那之前,一切都很好。

I ran rm -rf node_modulesand tried to reinstall, but no npmcommand was working.

我运行rm -rf node_modules并尝试重新安装,但没有任何npm命令起作用。

In the end I took the rather simple step of reinstalling Node via the official Node installer for Mac OS X.

最后,我采取了相当简单的步骤,通过 Mac OS X 的官方 Node 安装程序重新安装 Node。

https://nodejs.org/en/download/

https://nodejs.org/en/download/

Everything is fine now. Just went back to the directory, ran npm installand it's done the trick.

现在一切都很好。刚刚回到目录,运行npm install它就完成了。

回答by Telmo Trooper

On Arch Linuxwhat did the trick for me was:

Arch Linux 上,对我有用的是:

sudo pacman -Rs npm
sudo pacman -S npm

回答by Matthias Braun

Having just encountered this on Arch Linux 4.13.3, I solved the issue by simply reinstalling semver:

刚刚在 Arch Linux 4.13.3 上遇到了这个问题,我通过简单地重新安装解决了这个问题semver

pacman -S semver

回答by Shubham Jaiswal

Actually, it is taking the reference of previously stored modules.

实际上,它正在引用以前存储的模块。

Solution:Delete the npm-cache/npm folder in the installation directory of nodejs. In windows, it is in C:/User/Username/Appdata/Roaming/npm(or npm_cache). and try to install again.

解决方法:删除nodejs安装目录下的npm-cache/npm文件夹。在 Windows 中,它在 C:/User/Username/Appdata/Roaming/npm(或 npm_cache)中。并再次尝试安装。

Same thing to do in any OS.

在任何操作系统中都要做同样的事情。

回答by zeeawan

In my case on macOS(10.13.6), when I executed the following command

就我在 macOS(10.13.6) 上的情况而言,当我执行以下命令时

npm install -g react-native-cli

I got this error

我收到这个错误

Error: Cannot find module 'semver'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/unsupported.js:2:14)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

The error got resolved after executing the command

执行命令后错误得到解决

yarn global add npm

proposed by @Ashoor

由@Ashoor 提出

回答by the_haystacker

For me, this happened after I installed yarn globally. To resolve this issue, install npm using yarn and done.

对我来说,这是在我全局安装纱线之后发生的。要解决此问题,请使用 yarn 和 done 安装 npm。

yarn global add npm

回答by Sornakumar

On Windows, downloading Node's MSIagain and doing a 'Repair' worked for me.

在 Windows 上,再次下载Node 的 MSI并进行“修复”对我有用。