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

Created on 15 Sep 2017  路  39Comments  路  Source: coreybutler/nvm-windows

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

help wanted

Most helpful comment

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

All 39 comments

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 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 :(

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

image

@coreybutler just opened a new issue with the details

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

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

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

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

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

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

@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)

I have the same problem.

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

node 8.9.1
npm 5.5.1

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

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.

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

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.

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

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

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

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

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

Just ran into this issue today and this worked for me.
1) Open a bash terminal
2) CD into C:\Usersusername\AppData\Roamingnpm\node_modules\
3) should see a package.json
4) yarn install

@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\Roamingnvm\9.11.1npm 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.

I followed These steps and it solved my issue

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

installing the node through nvm 1.6.0 worked for me

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.

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.

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.

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

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.

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

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)

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

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.

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

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 ;)

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 馃槄

I followed These steps and it solved my issue

  1. uninstall Node.js
  2. Remove the content of the following folders
    C:\Users\AppData\Roamingnpm-cache
    C:\Users\AppData\Roamingnpm
    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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

snerte picture snerte  路  5Comments

eliavmaman picture eliavmaman  路  6Comments

keylowgee picture keylowgee  路  6Comments

webspecialist picture webspecialist  路  5Comments

thany picture thany  路  4Comments