nvm-windows icon indicating copy to clipboard operation
nvm-windows copied to clipboard

npm fails: "Cannot find module 'semver' (node v8.4.0)

Open lfurzewaddock opened this issue 6 years ago • 40 comments

If this is a question about how to use NVM4W, please use stackoverflow instead.

If this is an issue regarding antivirus, make sure you search the existing issues first.

My Environment

  • [ ] Windows 7 or below (not truly supported due to EOL - see wiki for details)

  • [ ] Windows 8

  • [ ] Windows 8.1

  • [X] Windows 10

  • [ ] Windows 10 IoT Core

  • [ ] Windows Server 2012

  • [ ] Windows Server 2012 R2

  • [ ] Windows Server 2016

  • [ ] My Windows installation is non-English.

I'm using NVM4W version:

  • [ ] 1.1.6
  • [X] 1.1.5
  • [ ] 1.1.4
  • [ ] 1.1.3
  • [ ] 1.1.2
  • [ ] 1.1.1
  • [ ] Older
  • [ ] OTHER (Please Specify)

I have already...

  • [X] read the README to be aware of npm gotchas & antivirus issues.
  • [X] reviewed the wiki to make sure my issue hasn't already been resolved.
  • [X] verified I'm using an account with administrative privileges.
  • [X] searched the issues (open and closed) to make sure this isn't a duplicate.
  • [X] made sure this isn't a question about how to use NVM for Windows, since gitter is used for questions and comments.

My issue is related to (check only those which apply):

  • [ ] settings.txt
  • [ ] proxy support (Have you tried version 1.1.0+?)
  • [ ] 32 or 64 bit support (Have you tried version 1.1.3+?)
  • [ ] Character escaping (Have you tried version 1.1.6+?)
  • [X] A standard shell environment (terminal/powershell)
  • [ ] A non-standard shell environment (Cmder, Hyper, Cygwin, git)

Expected Behavior

Return correct version number.

Actual Behavior

module.js:491 throw err; ^

Error: Cannot find module 'semver' at Function.Module._resolveFilename (module.js:489:15) at Function.Module._load (module.js:439:25) at Module.require (module.js:517:17) at require (internal/module.js:11:18) at Object. (C:\Users\leofu\AppData\Roaming\nvm\nvm\v8.4.0\node_modules\npm\lib\utils\unsupported.js:2:14) at Module._compile (module.js:573:30) at Object.Module._extensions..js (module.js:584:10) at Module.load (module.js:507:32) at tryModuleLoad (module.js:470:12) at Function.Module._load (module.js:462:3) module.js:491 throw err; ^

Error: Cannot find module 'semver' at Function.Module._resolveFilename (module.js:489:15) at Function.Module._load (module.js:439:25) at Module.require (module.js:517:17) at require (internal/module.js:11:18) at Object. (C:\Users\leofu\AppData\Roaming\nvm\nvm\v8.4.0\node_modules\npm\lib\utils\unsupported.js:2:14) at Module._compile (module.js:573:30) at Object.Module._extensions..js (module.js:584:10) at Module.load (module.js:507:32) at tryModuleLoad (module.js:470:12) at Function.Module._load (module.js:462:3)

Steps to reproduce the problem:

nvm install 8.4.0 nvm use 8.4.0 npm -v

lfurzewaddock avatar Sep 15 '17 20:09 lfurzewaddock

This is an npm error, not entirely an NVM error. I'd suggest reinstalling node 8.4.0.... perhaps the npm download was bad or something was corrupted.

coreybutler avatar Sep 15 '17 21:09 coreybutler

@coreybutler It won't work, keeps failing even deactivating, uninstalling, manually removing the folder reinstalling and reactivating. Same error.

I even copied the npm's node_modules dependencies from another working normal installation of node 8.5.0 and still having troubles. Could you look more into it, and see what the problem is?

The nvm is a really handy tool but not working anymore, and it's a pity :(

Zorgatone avatar Sep 26 '17 18:09 Zorgatone

Not sure what to tell you.... I can install semver just fine with the same setup.

image

coreybutler avatar Sep 26 '17 18:09 coreybutler

@coreybutler just opened a new issue with the details

Zorgatone avatar Sep 26 '17 18:09 Zorgatone

My own environment and the steps I used to replicate the issue here (closed duplicate): #305

Zorgatone avatar Sep 26 '17 18:09 Zorgatone

Same on the latest 8.8.1. Not sure how to fix this through NVM as what SO suggests is just clean re-install of node. Given the nature of nvm installs not sure how to replicate these steps. So the only way to use node v8 is to just not use NVM? https://stackoverflow.com/questions/33870520/npm-install-cannot-find-module-semver

Workaround

  1. nvm install 8.8.1 (or another version you want). Do not switch to it (yet).
  2. Download the *.zip of the same node version from official site (https://nodejs.org/en/download/current/)
  3. Substitute the contents of %APPDATA%/nvm/v8.8.1/npm for the npm folder from the downloaded archive
  4. nvm use 8.8.1
  5. Enjoy

roman-spiridonov avatar Oct 29 '17 09:10 roman-spiridonov

thanks @roman-spiridonov just ran into the same bug on node 8.8.1 installed via nvm on windows and your workaround seems to do the job

nmors avatar Oct 30 '17 04:10 nmors

Seems to have also worked using the latest version of node (9.1.0).

slandsaw avatar Nov 08 '17 22:11 slandsaw

seems npm thats installed from npm doesnt compatible with the node itself, you must download the zip and replace the npm module like @roman-spiridonov suggested

frizadiga avatar Nov 09 '17 15:11 frizadiga

@coreybutler is this not going to be addressed? I am seeing the exact same error exactly - anything above =< 8.0.

Unfortunately, I can't run npm i semver I get the same error.

hyndsite avatar Nov 11 '17 04:11 hyndsite

@roman-spiridonov I don't find that "npm" is a folder, but a file in the downloaded .zip. Replacing it with the downloaded one doesn't seem to resolve the semver issues for me (I know others have been successful)

hyndsite avatar Nov 11 '17 05:11 hyndsite

I have the same problem.

The @roman-spiridonov 's workaround work for me.

node 8.9.1
npm 5.5.1

talesporto avatar Nov 14 '17 12:11 talesporto

@hyndsite you need to look inside the node_modules directory - inside that is npm directory. Use that and it should work.

powellian avatar Nov 14 '17 22:11 powellian

Thanks @powellian I found that yesterday and was going to post a new update to @roman-spiridonov message - that the correct path is %APPDATA%/nvm/v8.8.1/node_modules/npm

The "npm" file in the /nvm/vx.x.x path helped to confuse the matter.

hyndsite avatar Nov 15 '17 12:11 hyndsite

Thank you, it works, But now I am wondering why is the package that nvm downloads & the one we download is different ?

benavern avatar Nov 17 '17 10:11 benavern

Same problem with 8.9.0 and @roman-spiridonov's workaround saved the day.

Like @benavern I'm curious why the package nvm installs is different from the one in the official repo.

Akamaozu avatar Jan 16 '18 00:01 Akamaozu

Same on the latest 8.9.4. The error was solved by deleting directory 'C:\Users\yourname\AppData\Roaming\nvm\v*' and re-executing 'nvm install'.

earth55 avatar Jan 31 '18 06:01 earth55

Workaround:

If you encounter this issue and you're using Windows 10, enable long paths using Registry Editor or Local Group Policy Editor.

Ref: https://github.com/coreybutler/nvm-windows/issues/305#issuecomment-345024594 Ref: https://superuser.com/a/1119948

But for me, I still needed to do @roman-spiridonov's workaround. However, I couldn't copy npm folder in Explorer, instead I needed to use robocopy:

robocopy "C:\Users\username\Downloads\node-v8.9.4-win-x64\node_modules\npm" "C:\Users\username\AppData\Roaming\nvm\v8.9.4\node_modules\npm" /mir

apptaro avatar Feb 15 '18 08:02 apptaro

alternative it to move nvm root. do nvm root C:\nvm before installing new node. might need to also copy extra files to new root. if already installed, then:

nvm uninstall 8.x.x
nvm root C:\nvm
copy /Y %APPDATA%\nvm\* C:\nvm
nvm install 8.x.x

jaxer avatar Feb 20 '18 14:02 jaxer

I experienced the same problem (Error: Cannot find module 'semver').

Some hints to get you going:

File compare (using official zip file) showed that there are indeed > 4k files and folders missing under node_modules\npm. It looks like the unzipping is aborted. Checked for v8.9.1 and v8.9.4: all node_modules alphabetically behind libnpx are missing. Wouldn't be surprised if it relates to those ridiculously nested folder structure: ...\nvm\v8.9.4\node_modules\npm\node_modules\libnpx\node_modules\yargs\node_modules\read-pkg-up\node_modules\read-pkg\node_modules\load-json-file\node_modules\parse-json\node_modules\error-ex\node_modules\is-arrayish\

I took the current master of nvm-windows, added some debug information to the install function within nvm.go, compiled it (with Go 1.10 win x64) and exchanged the nvm.exe with the new one. Result:

  • The npm zip file which is downloaded to the temp directory is in fact the official one. (@benavern , @Akamaozu )
  • The zip file was fully extracted within the temp directory and moved to the version-specific folder. No missing files.

Now for the surprising part, as I tried finding the regression:

  • Checked out tag 1.1.6, compiled (without any changes), replaced nvm.exe with the new one, deleted v8.9.4, ran nvm install 8.9.4 => successful, no missing files
  • Checked out tag 1.1.5, compiled (without any changes), replaced nvm.exe with the new one, deleted v8.9.4, ran nvm install 8.9.4 => successful, no missing files
  • Replaced nvm.exe with the original one from the official nvm-windows setup (1.1.5), deleted v8.9.4, ran nvm install 8.9.4 => still error, files missing.

It looks for me like there is an issue with the build process and/or environment.

Would be great if someone else could try if recreating the nvm.exe via build.bat or go build src/nvm.go solves the problem for him/her as well.

HTH, codeshell

codeshell avatar Feb 26 '18 01:02 codeshell

Delete npm folder from below folder C:\Users\username\AppData\Roaming\npm after => npm install Its Working Fine for me!....

LokeshNagarajan avatar Mar 17 '18 13:03 LokeshNagarajan

Just ran into this issue today and this worked for me.

  1. Open a bash terminal
  2. CD into C:\Users\username\AppData\Roaming\npm\node_modules\
  3. should see a package.json
  4. yarn install

cooploops avatar Apr 06 '18 18:04 cooploops

helped me: https://stackoverflow.com/questions/33870520/npm-install-cannot-find-module-semver

dev7ch avatar Apr 09 '18 16:04 dev7ch

@codeshell Thanks for your analysis. I found the issue to be due to the long paths that are not supported on Windows. For me even after downloading npm zip file and replacing contents of C:\Users\yourname\AppData\Roaming\nvm\9.11.1\npm I got the error. In fact I could not copy all files from downloaded npm zip file due to long names.

To avoid this I uninstalled node version 9.11.1, then uninstalled nvm. Reinstalled nvm at C:\nvm. Now my normal 'nvm install 9.11.1' works just fine and 'npm' is also in good state confirmed by running 'npm -v'.

I know this is a pretty drastic fix but just need to do one time.

kedarkolhatkar avatar Apr 21 '18 21:04 kedarkolhatkar

I followed These steps and it solved my issue

  1. uninstall Node.js
  2. Remove the content of the following folders C:\Users<user-name>\AppData\Roaming\npm-cache C:\Users<user-name>\AppData\Roaming\npm C:\Program Files\nodejs
  3. Then again install Node https://nodejs.org/en/

maheshigunaratne avatar May 01 '18 11:05 maheshigunaratne

installing the node through nvm 1.6.0 worked for me

BoyanPalagachev avatar May 17 '18 14:05 BoyanPalagachev

In my case, the paths were just too long. I chose c:\nvm as the install directory version manager and c:\nodejs for the symlink directory. That seemed to resolve the issue for me.

slandsaw avatar May 31 '18 00:05 slandsaw

I had the same problem with node v10.4.0 and the workaround from roman-spiridonov worked for me.

I had already removed %AppData%\npm etc. before installing nvm-windows.

leifurhauks avatar Jun 08 '18 14:06 leifurhauks

Tried getting 10.4.1 up and running, had problems at first.

Got it to work after deleting the folder from install location manually and installing via nvm again. Not sure whether this is too long of a path (obviously not) C:\Users\muser\AppData\Roaming\nvm\v10.4.1.

NicTorgersen avatar Jun 20 '18 18:06 NicTorgersen

Same problem with v10.5.0 and roman-spiridonovs solution worked for me as well.

rgomez90 avatar Jul 01 '18 04:07 rgomez90

I have the same probem when installing the latest node v10.6.0, then I just uninstalled it and switched to v8.11.3, problem solved.

I also tried download directly the npm-v6.1.0.zip, it do have some errors when unzipping.

zoubingwu avatar Jul 06 '18 14:07 zoubingwu

Experienced the same issue as @shadeofgod with 10.6.0. Switching to 8.11.3 solved it.

uilyam avatar Jul 08 '18 00:07 uilyam

problem persists with 10.6.0-64bit for a complete clean first install. (no node/npm before). roman-spiridonovs solution continues to work (for me the npm-folder can now be found in [...node]\node_modules\npm)

PutziSan avatar Jul 10 '18 12:07 PutziSan

Why is this still an issue after almost an entire year?

nickcoad avatar Jul 16 '18 11:07 nickcoad

Just encountered this issue on Windows 10.

Enabling Win32 long paths fixed the issue. See post from @apptaro (2018-02-15)

After enabling long paths, I rebooted Windows to make the change apply. I also had to delete the broken npm install manually.

After that, I could install the npm again using nvm.

I feel, nvm under Windows should issue warnings if long paths aren't enabled. nvm install also should not fail silently.

damianhofmannwork avatar Aug 29 '18 08:08 damianhofmannwork

For me:

  • nvm install 9.11.2 was the last version I found that worked through nvm.
  • Overwriting with the zip file from the website works on any version.
  • The long paths fix didn't seem to work (though I don't think I tried a machine restart after changing the registry setting).

This is the powershell script we use for new starters. Hopefully after a reboot they should be able to update to a later version is the idea.

# Get Chocolatey which is required for the rest of this script
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
choco feature enable -n=allowGlobalConfirmation
choco upgrade chocolatey

choco upgrade nvm
$env:NVM_HOME = [System.Environment]::ExpandEnvironmentVariables([System.Environment]::GetEnvironmentVariable("NVM_HOME","Machine"))
$env:NVM_SYMLINK = [System.Environment]::ExpandEnvironmentVariables([System.Environment]::GetEnvironmentVariable("NVM_SYMLINK","Machine"))
$env:Path = [System.Environment]::ExpandEnvironmentVariables([System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User"))

# Enable long paths in windows - may avoid npm issue https://github.com/coreybutler/nvm-windows/issues/302
New-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem' -Name 'LongPathsEnabled' -Value 1 -PropertyType DWORD -Force | Out-Null
New-ItemProperty -Path 'HKLM:\SYSTEM\ControlSet001\Control\FileSystem' -Name 'LongPathsEnabled' -Value 1 -PropertyType DWORD -Force | Out-Null

nvm install 9.11.2
nvm use 9.11.2

GrahamTheCoder avatar Aug 29 '18 09:08 GrahamTheCoder

Not sure what to tell you.... I can install semver just fine with the same setup.

image

I think running "npm install" would solve the problem, as it did with me ;)

momochanmo avatar Dec 04 '18 20:12 momochanmo

I believe @momochanmo had just forgotten to install the semver package in their project. It has the same error message but this is a totally different issue.

To recap: This thread is about the fact that nvm installs a corrupted version of npm with a missing/corrupted version of the semver package, which renders it inoperable.

The most likely root cause suggested so far is that a bug / behaviour change introduced into the Go compiler about 3 years ago causes an error that's being silently swallowed during installation. See https://github.com/coreybutler/nvm-windows/pull/370.

So if you know someone who works with Windows stuff and is enthusiastic about Go, now's a good time to tap them on the shoulder and offer them this excellent career development opportunity 😅

GrahamTheCoder avatar Dec 04 '18 21:12 GrahamTheCoder

I followed These steps and it solved my issue

  1. uninstall Node.js
  2. Remove the content of the following folders C:\Users\AppData\Roaming\npm-cache C:\Users\AppData\Roaming\npm C:\Program Files\nodejs
  3. Then again install Node https://nodejs.org/en/

Following this worked for me. You are a life saver!!!!! thanks <3

Pimentelk avatar Feb 01 '19 02:02 Pimentelk

I am using NVM for Node versions 10.21.1, 10.23.0, 12.20.0, 14.15.4

  • I cannot run "npm -v"

However, if I specify the actual location of npm - that works: C:\Users\cclark\AppData\Roaming\nvm\v12.20.1\npm -v

I have tried:

  1. delete directory of node version in C:\Users\cclark\AppData\Roaming\nvm\
  2. download zip file of node version from https://nodejs.org/download/release/v12.20.1/
  3. copy files from zip file to directory
  4. run 'nvm use 12.20.1'

npm -v will not run - semver error

nvm version 1.1.7 Windows version 10.0.17763

ChipClark avatar Feb 09 '21 00:02 ChipClark