Skip to content

Instantly share code, notes, and snippets.

@nathansmith
Last active July 18, 2017 15:15
Show Gist options
  • Save nathansmith/bb503b48ead20805b70775ebb6e9a160 to your computer and use it in GitHub Desktop.
Save nathansmith/bb503b48ead20805b70775ebb6e9a160 to your computer and use it in GitHub Desktop.
Force specific NPM version to be used, in a locked-down Windows environment.
@echo npm.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
@echo off
"c:\program files\nodejs\X.X.X\node" --max_old_space_size=4096 "c:\program files\nodejs\X.X.X\node_modules\npm\bin\npm-cli.js" %1 %2 %3 %4 %5 %6 %7 %8 %9
@echo
@nathansmith
Copy link
Author

nathansmith commented Jun 6, 2017

NOTE: I'm leaving instructions here, since comments aren't super intuitive in *.bat files:


I found myself with a conundrum today.

I was working in a remotely connected Windows VM, where we have the ability to request software be installed (and uninstalled) via IT department, but cannot ourselves do "admin" type actions such as uninstall.

I needed to force a specific version of NPM to be used, while there were multiple versions installed. However, Windows kept picking up on an out-of-date version as the default for npm run …. Without being able to manually remove that older version, I had to get creative.

My coworker showed me this trick, adding the hard-coded path for a desired NPM version into a npm.bat file.

When you cd into the project directory and type…

npm run whatever

…then Windows will see the npm.bat file and use it instead of the globally installed NPM.

With this file, we are basically telling Windows: Always use version X.X.X where those X's denote the semantic version.

http://semver.org

Essentially, this allows you to intercept the npm command, alias it on the fly to the correct version, and then continue running any other parameters that have been passed: %1, %2, etc.

(Add more as needed: %10, %11, …)

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