Npm install cannot find module 'semver'

2019-01-20 22:10发布

问题:

I can't use npm install using command prompt in NodeJS. I'm getting this errors when running 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)

Please help me.

回答1:

I had the same error. npm uninstall npm -g, rm -rf node_modules didn'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):

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



回答2:

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



回答3:

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

I ran rm -rf node_modules and tried to reinstall, but no npm command was working.

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

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

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



回答4:

I'm facing the same issue here.

If this occurs right after you run brew install yarn try running yarn global add npm and voilà - fixed!



回答5:

On Arch Linux what did the trick for me was:

sudo pacman -Rs npm
sudo pacman -S npm


回答6:

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

pacman -S semver


回答7:

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. Same thing to do in any OS.



回答8:

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



回答9:

Faced this issue when I ran npm install using a lower version of node. Then later when I upgraded to latest version of node and ran gulp, it has shown this error.

To resolve, deleted node_modules and re-ran npm install. Then gulp ran fine.



回答10:

Just remove the /node_modules folder and try again. ;)

You can use npm rm to remove it.

You can also check this issue on GitHub. To be more specific about unnistal, try to follow this:

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.



回答11:

I had the same issue but it was caused by a broken package-lock.json file.

Deleting package-lock.json and running npm install again fixed it for me.



回答12:

I finally figured this out! Yes, you have to uninstall/delete all files and reinstall. BUT SOME OF THE FILE PATHS ARE TOO LONG TO DELETE DURING THE UNINSTALL PROCESS. The file paths are so long in Node that they may not be able to be deleted. You have to rename the file paths.

After uninstalling Node, use Command Prompt, navigate to ...\Appdata\roaming\ (use the path from the error message). Try to delete the folder with del npm. Then, if it doesn't allow you to delete a folder, start renaming the folders in the file path in the next error message to "a" or something short. You may have to do this for multiple folders along the path. Then you should be able to delete! Then reinstall node and it should work.



回答13:

In my case, simply re-running brew install yarn fixed the problem.



回答14:

I faced with same trouble after brew installs yarn on my MacOs Sierra 10.12.6. (And the first try of installation fall after brew update self with a message " Homebrew must be run under Ruby 2.3! You're running 2.0.0. (RuntimeError)". So I re-run it.)

So I use n manager to reinstall npm.

  • switch to another node version (for my case to 8.0) by "n" command with empty parameters
  • or install new by "n x.x.x" where x.x.x the node version number (i need 6.9.1)
  • delete working version by "n rm x.x.x" where x.x.x the node version number. use sudo if need
  • install working version by "n x.x.x" where x.x.x the node version number. use sudo if need
  • switch by to the working version of node. (see first step for reference)

I think the trouble can be caused that node was installed under sudo for case.



回答15:

Just check your preinstall scripts if you have one. Sometimes to restrict the versions of node and npm one needs to run a project.

If that's the case you need to install semver manually via npm install -g semver



回答16:

I ran into the same issue and fixed it by upgrading my Node version