我不能在NodeJS中使用命令提示符使用npm install。我在运行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)

当前回答

在我的情况下,简单地重新运行brew install yarn解决了这个问题。

其他回答

我也遇到过同样的问题,我解决它的方法是执行

PATH="$PATH"

在终端中,问题是终端保留了旧的nodejs路径,而不是使用新的路径。

我也犯了同样的错误。npm uninstall npm -g, rm -rf node_modules没有帮助我,因为当我尝试时,我得到错误:无法找到模块'semver'。但是我用这些步骤解决了我的问题(这将删除你可能正在使用的其他全局模块):

Sudo rm -rf /usr/local/lib/node_modules Sudo rm -rf ~/.npm 酿造卸载——force节点 Brew安装节点

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

在Ubuntu上,如果你想通过默认的存储库使用apt来管理Node,最好同时安装nodejs和npm。

你正在使用的npm版本很有可能在某个特定的位置期望与你已经安装的Node JS版本不一致。如果只有一个版本的nodejs和一个版本的npm,并且它们都是使用默认存储库安装的,这应该可以完美地工作。

下面是我的最佳指导,让Node和npm使用Ubuntu的主要存储库工作:

sudo apt-get purge nodejs --auto-remove and sudo apt-get purge npm --auto-remove whereis node: remove all the versions of node, one at a time. Repeat until this command returns a blank after node:. sudo apt-get install nodejs node --version: should return ... no such file or directory nodejs --version: should return v1x.x.x. This is the version installed by apt. It will probably be less recent that the latest version available on the Node JS website. sudo apt-get install npm npm --version: it will use the nodejs installed by apt and work correctly.

对我来说,它发生在我的项目从gulp 3升级到gulp 4的时候。 所以,当我运行“gulp watch”命令时,它抛出了同样的错误。

这可能是由于与我的全球安装的gulp版本不匹配。

因此,要么用“-g”全局标志升级gulp会有所帮助,要么我按照下面的方法进行

但是,我在我的项目中使用了“./node_modules/.bin/gulp watch”。这确保了gulp需要的所有依赖项都只取自node_modules。

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