DEV Community

Cover image for Mastering Node.js Version Management with Fast Node Manager (fnm)
Aditya
Aditya

Posted on

Mastering Node.js Version Management with Fast Node Manager (fnm)

Introduction to Node.js Version Management

Managing multiple Node.js versions can be challenging for developers who work on different projects with varying runtime requirements. While traditional installers limit you to a single Node.js version, Fast Node Manager (fnm) provides a flexible, efficient solution for switching between Node.js versions seamlessly.

What is fnm?

fnm is a fast and simple Node.js manager built in Rust ⚑

Why Use fnm?

Before diving into the installation and usage, let's understand the advantages of fnm:

  1. Quick Version Switching: Instantly change Node.js versions without complex uninstallation processes.
  2. Project-Specific Configurations: Set different Node.js versions for different projects.
  3. Lightweight and Fast: Unlike some other version managers, fnm is designed to be minimal and performant.
  4. Cross-Platform Support: Works consistently across Windows, macOS, and Linux.

Step-by-Step Guide to Installing fnm

1. Installation Methods

For macOS and Linux:

# Using curl
curl -fsSL https://fnm.vercel.app/install | bash

# Alternative method using shell script
wget -qO- https://fnm.vercel.app/install | bash
Enter fullscreen mode Exit fullscreen mode

For Windows:

# Using winget
winget install Schniz.fnm

# Using scoop
scoop install fnm
Enter fullscreen mode Exit fullscreen mode
eval "$(fnm env --use-on-cd)"
Enter fullscreen mode Exit fullscreen mode

2. PowerShell Configurations

Add the following to the end of your PowerShell profile file :

fnm env --use-on-cd --shell powershell | Out-String | Invoke-Expression
Enter fullscreen mode Exit fullscreen mode

Note

You have to find the Powershell Profile file or create one if it's not already present, you can find the instructions below

For macOS/Linux, the profile is located at

~/.config/powershell/Microsoft.PowerShell_profile.ps1
Enter fullscreen mode Exit fullscreen mode

For Windows location is either:

%userprofile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 
Enter fullscreen mode Exit fullscreen mode

Powershell 5

%userprofile%\Documents\PowerShell\Microsoft.PowerShell_profile.ps1 
Enter fullscreen mode Exit fullscreen mode

If the profile is not present and to create the profile file you can run this in PowerShell:

if (-not (Test-Path $profile)) { New-Item $profile -Force }
Enter fullscreen mode Exit fullscreen mode

To edit your profile run this in PowerShell:

Invoke-Item $profile
Enter fullscreen mode Exit fullscreen mode

Invoke it and then add the first PowerShell command to the end of your PowerShell Profile

This enables automatic Node.js version switching and you can easily use multiple node js versions.

Basic fnm setup flow

# Download and install fnm:
winget install Schniz.fnm
# Download and install Node.js:
fnm install 22
# Select the version
fnm use 22  
# Verify the Node.js version:
node -v # Should print "v22.13.1".
# Verify npm version:
npm -v # Should print "10.9.2".
Enter fullscreen mode Exit fullscreen mode

If you face any error such as "can't find fnm's environment variables", then it may be related to configurations of the shell you are using!

Installing Node.js Versions

# Install the latest LTS version
fnm install --lts
fnm i --lts

# Install a specific version
fnm install 16.14.2

# Install the latest version
fnm install latest
Enter fullscreen mode Exit fullscreen mode

Managing Installed Versions

# List all installed Node.js versions
fnm ls

# List all remote Node.js versions
fnm ls-remote

# Set a default global Node.js version
fnm default 22.13.1

# Use a specific version in the current shell
fnm use 22.13.1
Enter fullscreen mode Exit fullscreen mode

fnm remote

Current Node Version

fnm current
Enter fullscreen mode Exit fullscreen mode

fnm current

Version Aliasing

# Syntax to set an alias for a version is
fnm alias <version> <name>
fnm alias 22.13.1 my-nodeproject

# Use the aliased version
fnm use my-nodeproject

# To set the default alias 
fnm default 22.13.1

# To Unalias 
fnm unalias <name>
fnm unalias my-nodeproject

Enter fullscreen mode Exit fullscreen mode

Project-Specific Version Management

Create a .node-version file in your project root to automatically use a specific Node.js version:

# In your project directory
echo "22.13.1" > .node-version
Enter fullscreen mode Exit fullscreen mode

Now, when you enter the project directory, fnm will automatically switch to the specified version.

Best Practices

  1. Always use LTS (Long Term Support) versions for production projects.
  2. Regularly update fnm to get the latest features and improvements.
  3. Use .node-version or .nvmrc files to maintain consistency across development teams.

Troubleshooting

Again, If you encounter issues:

  • Ensure fnm is correctly added to your PATH
  • Verify shell configuration
  • Check fnm version with fnm --version

Conclusion

Fast Node Manager simplifies Node.js version management, offering developers a flexible, efficient tool for handling multiple runtime environments. By following these steps, you can easily switch between Node.js versions and maintain project-specific configurations.

If you liked this article, drop a like or comment ** ❀ or **maybe share it in your community :). You can also drop me a follow on X 🐀 or Linked In πŸ‘¨β€πŸ«

Top comments (0)