Michael Charles Aubrey

https://MichaelCharl.es/Aubrey

Installing fnm on Windows for PowerShell, Git Bash, and Command Prompt

Photo by Bill Jelen | Unsplash

Installing fnm on Windows for PowerShell, Git Bash, and Command Prompt

Michael Charles Aubrey // Thu Jan 23 2025

fnm (Fast Node Manager) lets you easily manage multiple Node.js versions. While installation is simple, configuring it across different Windows shell requires additional setup.

You can install fnm from either winget or chocolatey:

winget install Schniz.fnm
# or
choco install fnm

From there, we need to install the specific version of Node you want to use. I’m going to use 22 as an example.

fnm install 22

Now, if you open a new shell and run node --version, you'll get the error, "The term 'node' is not recognized as the name of a cmdlet, function, script file, or operable program."

The term 'node' is not recognized as the name of a cmdlet, function, script file, or operable program.

This happens because fnm needs to modify your PATH dynamically when switching Node versions. Setting a static PATH in the Windows environment variables isn't enough.

The solution is to run fnm env --use-on-cd at the start of each shell session.

PowerShell

Open PowerShell as an administrator. To do this, you can right-click the Start button and select "Windows PowerShell (Admin)".

First, enable script execution if you haven't already.

Set-ExecutionPolicy -Scope CurrentUser RemoteSigned

Create or modify your PowerShell profile:

notepad $PROFILE

If your PowerShell profile already exists, that should open it. If not, it will create a new profile file for you, in which case it will be blank.

Add this line to the end of your profile.

fnm env --use-on-cd --shell powershell | Out-String | Invoke-Expression

Save and close the file. The profile is reloaded automatically each time you start a new PowerShell session, so you need to close and open PowerShell again for it to take effect.

You can verify that it worked by running node --version. The current version, in my case "v22.13.1", should be displayed.

Git Bash

The following configuration needs to be done in Git Bash, so start a new Git Bash session.

Create or modify your .bashrc. The reason why we’re running touch first is because if the file does not exist, Notepad will automatically create it as .bashrc.txt, which is not what we want. If ~/.bashrc already exists, touch will do nothing.

touch ~/.bashrc
notepad ~/.bashrc

Add this line to the end of your ./bashrc file:

eval "$(fnm env --use-on-cd --shell bash)"

Same as PowerShell, you'll need to close and open Git Bash for the changes to take effect. Start a new Git Bash session and verify with node --version.

Command Prompt

Command Prompt doesn't have profile scripts like other shells. Instead, we can use the registry to recreate a similar kind of behavior.

Despite the fact that we're configuring Command Prompt, we'll be using PowerShell to set it up, so start a new PowerShell session.

First, create a profile.cmd in your user directory. In the following script, be sure to replace "XXX" with your Windows username.

$profileContent = @"
@echo off
if not defined FNM_AUTORUN_GUARD (
    set "FNM_AUTORUN_GUARD=AutorunGuard"
    FOR /f "tokens=*" %%z IN ('fnm env --use-on-cd') DO CALL %%z
)
"@

Set-Content -Path "C:\Users\XXX\profile.cmd" -Value $profileContent

Then, run the following command to set the AutoRun registry key. Again, be sure to replace "XXX" with your Windows username.

Set-ItemProperty -Path "HKLM:\Software\Microsoft\Command Processor" -Name "AutoRun" -Value "C:\Users\XXX\profile.cmd"

Start a new Command Prompt and verify with node --version.