Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A way to update npm ? #13

Closed
vkbansal opened this issue Oct 15, 2014 · 17 comments
Closed

A way to update npm ? #13

vkbansal opened this issue Oct 15, 2014 · 17 comments

Comments

@vkbansal
Copy link

No description provided.

@coreybutler
Copy link
Owner

Just run npm update npm... same as without NVM for Windows.

@filipesilva
Copy link

Heya, I just wanted to add that upgrading updating npm isn't quite as simple as just npm update npm:

$ npm update npm -g
npm ERR! path C:\Program Files\nodejs\npm.cmd
npm ERR! code EEXIST
npm ERR! Refusing to delete C:\Program Files\nodejs\npm.cmd: is outside C:\Program Files\nodejs\node_modules\npm and not a link
npm ERR! File exists: C:\Program Files\nodejs\npm.cmd
npm ERR! Move it away, and try again.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\kamik\AppData\Roaming\npm-cache\_logs\2017-08-24T07_56_26_609Z-debug.log

(I used -g here but I assume that's what you meant since you didn't say anything about going into the global directory. But if that is what you meant, I'm just describing the same thing in more detail.)

Here is a list of the things you need to do when not using NVM on the official npm wiki: https://github.com/npm/npm/wiki/Troubleshooting#upgrading-on-windows.

But there's actually an easier way if you're using NVM. Since NVM links both binaries and folders, you can just go and update the original global node modules manually. This doesn't require neither admin permissions nor moving files around.

First run node -v and take note of the version of node you're using (e.g v6.11.1). Then do this:

cd %appdata%\nvm\6.11.1
npm install npm@latest --no-save

The --no-save option is important to prevent npm from trying to find package.json in that directory, and from auto creating package-lock.json (in npm5 and up).

@raikesy
Copy link

raikesy commented Jan 3, 2019

I just spent hours trying to pinpoint the cause of this error before coming across this comment. Maybe this workaround should be displayed more clearly or this issue reopened as I'm sure trying to update npm with the standard command is quite a common operation and currently it fails and gives the very esoteric error message shown above.

@createdbyjurand
Copy link

I think this is more of a previous npm version bug than nvm one.

However, to update npm delete in nvm/[node version] folder (for me it is v10.15.3) following files:

C:\Users\[user name]\AppData\Roaming\nvm\[node version]\npm
C:\Users\[user name]\AppData\Roaming\nvm\[node version]\npm.cmd
C:\Users\[user name]\AppData\Roaming\nvm\[node version]\npx
C:\Users\[user name]\AppData\Roaming\nvm\[node version]\npx.cmd

Rename npm folder to npm2

C:\Users\[user name]\AppData\Roaming\nvm\[node version]\node_modules\npm2

Open any console with admin rights (or even right mouse click on vscode shortcut, than right mouse click on Visual Studio Code and hit Run as administrator) and type commands:

cd C:\Users\[user name]\AppData\Roaming\nvm\[node version]\node_modules\npm2\bin
node npm-cli.js i -g npm@latest

Done.

Delete C:\Users\[user name]\AppData\Roaming\nvm\[node version]\node_modules\npm2 folder.

@alexisCata
Copy link

alexisCata commented Mar 28, 2019

I think this is more of a previous npm version bug than nvm one.

However, to update npm delete in nvm/[node version] folder (for me it is v10.15.3) following files:

C:\Users\[user name]\AppData\Roaming\nvm\[node version]\npm
C:\Users\[user name]\AppData\Roaming\nvm\[node version]\npm.cmd
C:\Users\[user name]\AppData\Roaming\nvm\[node version]\npx
C:\Users\[user name]\AppData\Roaming\nvm\[node version]\npx.cmd

Rename npm folder to npm2

C:\Users\[user name]\AppData\Roaming\nvm\[node version]\node_modules\npm2

Open any console with admin rights (or even right mouse click on vscode shortcut, than right mouse click on Visual Studio Code and hit Run as administrator) and type commands:

cd C:\Users\[user name]\AppData\Roaming\nvm\[node version]\node_modules\npm2\bin
node npm-cli.js i -g npm@latest

Done.

Delete C:\Users\[user name]\AppData\Roaming\nvm\[node version]\node_modules\npm2 folder.

The only solution that worked for me.

@eroSilva
Copy link

eroSilva commented May 5, 2019

It worked for me, too. Thanks! :)

@Jafferwaffer
Copy link

Awesome help @createdbyjurand cheers

@8thHalfHour
Copy link

Thank you, @createdbyjurand ! I have been pulling my hair out trying to figure out why the regular commands were not working. This is fantastic! 💯

@richteambs
Copy link

Thanks to @createdbyjurand !

I needed to update in C:\ProgramData\nvm\ rather than C:\Users\[user name]\AppData\Roaming\nvm in case that helps anyone else.

@8thHalfHour
Copy link

8thHalfHour commented Jul 19, 2019

Thank you so very much to @createdbyjurand. This worked flawlessly! I wrote a script for it for PowerShell, if anyone wants it; you can run it form anywhere as long as you update the node version being updated (substitute both instances of Username and Version of Node with your own):

cd C:\Users\<Username>\AppData\Roaming\nvm\<Version of Node>
Remove-Item npm
Remove-Item npm.cmd
Remove-Item npx
Remove-Item npx.cmd
cd node_modules
Rename-Item npm -NewName npm-old
cd npm-old\bin
node npm-cli.js install -g npm@next
cd C:\Users\<Username>\AppData\Roaming\nvm\<Version of Node>\node_modules
Remove-Item -Recurse -Force npm-old

Run with the usual .\<filename>.ps1 from anywhere. 😎

@raikesy
Copy link

raikesy commented Aug 21, 2019

The solution outlined by @createdbyjurand does work but has to be repeated every time you install a new version of node. It also causes the following error message updating npm after the initial workaround:

js-algos-structures $ npm install -g npm@latest
C:\Users\jzrai\scoop\apps\nvm\current\nodejs\nodejs\npx -> C:\Users\jzrai\scoop\apps\nvm\current\nodejs\nodejs\node_modules\npm\bin\npx-cli.js
C:\Users\jzrai\scoop\apps\nvm\current\nodejs\nodejs\npm -> C:\Users\jzrai\scoop\apps\nvm\current\nodejs\nodejs\node_modules\npm\bin\npm-cli.js
+ npm@6.11.1
updated 2 packages in 32.004s

C:\Users\jzrai\projects\js-algos-structures>npm-cli.js" install -g npm@latest
'npm-cli.js" install -g npm@latest' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\jzrai\projects\js-algos-structures>ENDLOCAL

C:\Users\jzrai\projects\js-algos-structures>EXIT /b 

I don't understand the inner workings enough to know if this is a bug with nvm, npm or scoop but, as it seems to be nvm, I think this issue should be reopened @coreybutler as updating npm is quite a fundamental operation which is currently completely broken with this set-up.
At the very least @createdbyjurand's workaround should be documented more visibly.
If this is definitely a bug with npm I will happily post an issue there.

@musaab-abdalla
Copy link

I just updated with npm install -g npm@latest

  • cd to current node directory C:\Users\user_name\AppData\Roaming\nvm\v12.8.1
  • run npm install -g npm@latest
    Working great Thank you @filipesilva

@coreybutler
Copy link
Owner

@raikesy - npm and nvm have no operational relationship... meaning nvm just leaves npm alone. It merely downloads the npm archive and extracts it to the appropriate installation folder. That's it.

NVM4W doesn't touch the executable files. Unlike nvm for *nix, there is no shim. When node.exe is executed, it's actually calling that file. There is literally nothing else running. npm calls node.exe internally whenever it executes a script, i.e. node npm-cli.js.

Tools like scoop, homebrew, chocolatey, etc may or may not modify behavior. I don't really know because I don't maintain those. However; I don't think any of those are the problem because I can replicate these problems with standard Node installs (i.e. no nvm4w, no package manager, just a straight up msi install from nodejs.org).

@createdbyjurand is correct that this behavior often comes from older versions of npm. npm has gone through a number of rather difficult design decisions, especially with versions shipping in the Node 8.x.x line where there were some hard-coded path files used for installation.

@roa-nyx
Copy link

roa-nyx commented Oct 22, 2019

@coreybutler this might be more headache than it is worth, but as you say

It merely downloads the npm archive and extracts it to the appropriate installation folder. That's it.

Couldn't we then say that NVM's responsibility starts (and ends) at the installation of NPM? If so I feel like my concerns can be addressed by having something like nvm update-npm latest or something similar. This would in theory take the current version of node and install the specified version of npm to it. It could use a method similar to what's being outlined by @createdbyjurand or something simpler.

You mentioned that you also verified an issue exists in the official node release MSI install. Can you clarify what issue you verified? Was it with an older MSI installation that shipped with an older NPM? I'm running a clean system that hasn't had any node or npm installed prior to NVM and I'm running into these issues.

@erikstagg
Copy link

@createdbyjurand I updated your script so that you don't have to edit the script to run it, even when switching versions of node. I also took out @next because that installs a pre-release (non-stable).

$starting_path = Get-Location
$node_version = node -v
$node_path = "$env:appdata\nvm\$node_version"
Write-Host "node_version: $node_version"
Write-Host "Path used: $node_path"
cd $node_path
Remove-Item npm
Remove-Item npm.cmd
Remove-Item npx
Remove-Item npx.cmd
cd node_modules
Rename-Item npm -NewName npm-old
cd npm-old\bin
node npm-cli.js install -g npm
cd ../..
Remove-Item -Recurse -Force npm-old
cd $starting_path

@sediq-khan
Copy link

I am using Windows 10 and I spent a whole day on updating pm. Nothing worked but following.

https://www.wdiaz.org/update-npm-when-node-was-installed-using-chocolatey-on-windows/

Steps 1: Copy and Paste the following lines into a file somewhere in your directories and give it the .cmd extension.
Step 2: Double click the newly created "above" file.

You should be good to go now with the most up to date version of node.

@KillyMXI
Copy link

KillyMXI commented May 2, 2021

Windows 10, PowerShell:

PS> nvm list
PS> cd "$env:APPDATA\nvm\v99.99.9"
PS> npm install npm --no-save

(Change digits to the node version where you want to update npm.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests