我不能在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)

当前回答

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

npm install -g react-native-cli

我得到了这个错误

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)

执行命令后错误得到解决

yarn global add npm

由@Ashoor提议

其他回答

只需删除你的node_modules

rm -rf node_modules

than

yarn

or

npm install

我也有这个,在昨天运行brew安装纱线后。至少,在那之前一切都很好。

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

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

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

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

只需删除(或重命名)克隆库的semver文件夹版本: “gitClonedRepository / App / node_modules / semver”

复制“semver”文件夹 “C: \ Program Files \ nodejs \ node_modules \ npm \ node_modules \ semver”

进入应用程序的node_modules文件夹。

(如果你在Windows上,否则它必须位于/usr/local/lib/node_modules)。

只需删除/node_modules文件夹,然后重试。,)

你可以使用npm rm来删除它。

你也可以在GitHub上检查这个问题。 更具体地说,unnistal是这样的:

Uninstalling So sad to see you go. sudo npm uninstall npm -g Or, if that fails, sudo make uninstall More Severe Uninstalling Usually, the above instructions are sufficient. That will remove npm, but leave behind anything you've installed. If you would like to remove all the packages that you have installed, then you can use the npm ls command to find them, and then npm rm to remove them. To remove cruft left behind by npm 0.x, you can use the included clean-old.sh script file. You can run it conveniently like this: npm explore npm -g -- sh scripts/clean-old.sh npm uses two configuration files, one for per-user configs, and another for global (every-user) configs. You can view them by doing: npm config get userconfig # defaults to ~/.npmrc npm config get globalconfig # defaults to /usr/local/etc/npmrc Uninstalling npm does not remove configuration files by default. You must remove them yourself manually if you want them gone. Note that this means that future npm installs will not remember the settings that you have chosen.

对我来说,这发生在我在全球安装纱线之后。 为了解决这个问题,使用yarn安装npm。

yarn global add npm