Switch Node Versions According to Project

Walter Staeblein
2 min readSep 4, 2024

--

If you develop NodeJS apps on Windows and work on projects that rely on different versions of Node, there is this quick way of automating the process of running your apps in dev mode without the hassle of changing Node’s version everytime you switch to a different project or having to install third party components such as AVN to do the same.

Only 3 simple steps:

1- install NVM if you not already using it.

2 — Create a batch file in your project’s root (same folder as package.json). Name it run.bat

3- Copy the Windows Batch code below into to the bat file you just created and save it.

:: Check if NodeJS is the right version before running the app
@echo off
(node --version & echo.) >2 & (set /p ver=)<2
if NOT %ver:~1,2% == 18 (goto changenode) else (goto runapp)

:changenode
nvm use 18
:: Wait a second for things to fall into place
timeout /t 1

:runapp
npm run dev

This batch file will check the current NodeJS’ version and change it as needed.

In this example it is changing the version if it is not 18.x.x. To projects that need a different version just change the 18 in the if line and nvm run line to your required version.

Should you need to work with full versions (here I’m using just the main version number), change the following lines:

if NOT %ver% == v18.19.1 (goto changenode) else (goto runapp)
...
nvm use 18.19.1

In my projects there is always a run.bat file on the root folder so I just type run to execute any of then from my IDE’s terminal, even on workstations that have only one version of Node. Faster & simpler!

--

--