Skip to content

Instantly share code, notes, and snippets.

@johnmcase
Last active October 6, 2022 16:28
Show Gist options
  • Save johnmcase/d31b799b9030327091a0e74880e4c530 to your computer and use it in GitHub Desktop.
Save johnmcase/d31b799b9030327091a0e74880e4c530 to your computer and use it in GitHub Desktop.
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
)
)
)
@darthvader666uk
Copy link

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