Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Update npm on windows
rem see https://github.com/coreybutler/nvm-windows/issues/300
@echo off
SETLOCAL EnableDelayedExpansion
if [%1] == [] (
echo Pass in the version you would like to install, or "latest" to install the latest npm version.
) else (
set wanted_version=%1
if "!wanted_version!" == "latest" (
for /f %%i in ('npm show npm version') do set wanted_version=%%i
)
for /f %%i in ('npm -g -v') do set cur_version=%%i
if "!cur_version!" == "!wanted_version!" (
echo Already on npm version !wanted_version!.
) else (
echo Updating to !wanted_version!...
rename "!NVM_SYMLINK!\npm" npm2
rename "!NVM_SYMLINK!\npm.cmd" npm2.cmd
rename "!NVM_SYMLINK!\node_modules\npm" npm2
node "!NVM_SYMLINK!\node_modules\npm2\bin\npm-cli.js" i npm@!wanted_version! -g --force
for /f %%i in ('npm -g -v') do set new_version=%%i
echo New version installed is !new_version!
if "!new_version!" == "!wanted_version!" (
echo Successfully updated to !wanted_version!. Cleaning up backups...
del "!NVM_SYMLINK!\npm2"
del "!NVM_SYMLINK!\npm2.cmd"
@RD /S /Q "!NVM_SYMLINK!\node_modules\npm2"
echo Update complete.
) else (
echo Something went wrong. Rolling back.
if exist "!NVM_SYMLINK!\npm" (
del "!NVM_SYMLINK!\npm"
)
if exist "!NVM_SYMLINK!\npm.cmd" (
del "!NVM_SYMLINK!\npm.cmd"
)
if exist "!NVM_SYMLINK!\node_modules\npm" (
@RD /S /Q "!NVM_SYMLINK!\node_modules\npm"
)
rename "!NVM_SYMLINK!\npm2" npm
rename "!NVM_SYMLINK!\npm2.cmd" npm.cmd
rename "!NVM_SYMLINK!\node_modules\npm2" npm
)
)
)
@pr1sm
Copy link

pr1sm commented Jun 3, 2018

This is awesome! I did have to make one change however:
I originally had nvm-windows install versions of node to a second drive (E:\Program Files\nodejs rather than C:\Program Files\nodejs). This broke the usage of PROGRAMFILES on line 22. However, nvm-windows sets the install directory for node as NVM_SYMLINK. This is the correct path regardless of what drive is used.

Other than that, worked like a charm!

You can see my fix in the fork I made here: https://gist.github.com/pr1sm/240466ea9b3e8ad9daedbb8efb0f1b72

@g8up
Copy link

g8up commented Oct 13, 2018

In my case, my node is in D:\programe files, and I add a line before set node_path:

    set PROGRAMFILES=D:\progra~1
    set node_path=!PROGRAMFILES!\nodejs

btw, as I set the variable to "D:\programe files", the blank in the path will break the string.

@gijswijs
Copy link

gijswijs commented Jan 14, 2019

This just saved me a ton of work. Thanks!

@ststeiger
Copy link

ststeiger commented Mar 21, 2019

NodeJS doesn't have to be in !PROGRAMFILES!
In fact, you can put it anywhere where you have write-access when you don't have admin rights, you just need to add it to the path environment variable.
So here's how to properly find the node path:

  • split path by ";"
  • foreach folder in %path% => if folder contains "node" (case_insensitive) => node_path is this folder
  • it node path not in %path%, assume !PROGRAMFILES!\nodejs

Proper way would be to search the registry as well, assuming there is any entry there (HKLM or HKCU if installed without admin rights)

@echo off


set node_path=!PROGRAMFILES!\nodejs

for %%A in ("%path:;=";"%") do (
    REM echo "%%~A"
    
    echo %%~A|find /i "node" >nul
    if not errorlevel 1 (
        set node_path=%%~A
    ) 


)


echo "Node-Path: %node_path%"

Full script:
https://gist.github.com/ststeiger/8d3588a8864dd0a2f82ad55adc48a39c

Copy link

ghost commented Apr 22, 2019

Works great, thank you!

@mpotoc
Copy link

mpotoc commented Mar 16, 2020

i am getting this error, whether i run this script as admin or not:

Updating to 6.14.2...
npm ERR! code EEXIST
npm ERR! path C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js
npm ERR! dest C:\Program Files\nodejs\npm.ps1
npm ERR! EEXIST: file already exists, cmd shim 'C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js' -> 'C:\Program Files\nodejs\npm.ps1'
npm ERR! File exists: C:\Program Files\nodejs\npm.ps1
npm ERR! Remove the existing file and try again, or run npm
npm ERR! with --force to overwrite files recklessly.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\mpoto\AppData\Roaming\npm-cache_logs\2020-03-16T11_48_11_629Z-debug.log
'npm' is not recognized as an internal or external command,
operable program or batch file.
New version installed is
Something went wrong. Rolling back.

can someone tell me what is wrong? i checked the log file, but there is the same error inside

@darthvader666uk
Copy link

darthvader666uk commented Nov 26, 2020

NodeJS doesn't have to be in !PROGRAMFILES!
In fact, you can put it anywhere where you have write-access when you don't have admin rights, you just need to add it to the path environment variable.
So here's how to properly find the node path:

* split path by ";"

* foreach folder in %path% =>  if folder contains "node" (case_insensitive) => node_path is this folder

* it node path not in %path%, assume !PROGRAMFILES!\nodejs

Proper way would be to search the registry as well, assuming there is any entry there (HKLM or HKCU if installed without admin rights)

@echo off


set node_path=!PROGRAMFILES!\nodejs

for %%A in ("%path:;=";"%") do (
    REM echo "%%~A"
    
    echo %%~A|find /i "node" >nul
    if not errorlevel 1 (
        set node_path=%%~A
    ) 


)


echo "Node-Path: %node_path%"

Full script:
https://gist.github.com/ststeiger/8d3588a8864dd0a2f82ad55adc48a39c

This was spot on! I use Laragon on WIndows and this worked a treat. thank you

@keanerickson-mc
Copy link

keanerickson-mc commented Dec 30, 2021

node "!NVM_SYMLINK!\node_modules\npm2\bin\npm-cli.js" i npm@!wanted_version! -g --force

This line failed for me:

Error: Cannot find module 'C:\node_modules\npm2\bin\npm-cli.js'

But npm install -g npm@latest worked afterwards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment