w3resource

Managing Node.js Versions with nvm Made Simple


Using Node Version Manager (nvm) with Node.js

Node Version Manager (nvm) is a popular tool that allows developers to manage multiple versions of Node.js on their system. With nvm, you can easily install, switch, and use different Node.js versions, ensuring compatibility with various projects. This is particularly useful when working on multiple projects requiring different Node.js versions or when testing applications with newer versions.


Syntax:

Below are the key commands for using nvm:

1. Install the latest Node.js version:

nvm install node

2. Install a specific Node.js version:

nvm install <version>

3. Use a specific Node.js version:

nvm use <version>

4. List installed Node.js versions:

nvm list

5. Set a default Node.js version:

nvm alias default <version>

Examples and Code

1. Installing nvm on Your System

Code:

# Install nvm using the official installation script
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash

# OR, for systems with wget
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash

Explanation:

  • The curl or wget command fetches the installation script for nvm.
  • After installation, restart your terminal or source your shell configuration file:

2. Installing the Latest Node.js Version

Code:

# Install the latest version of Node.js
nvm install node

Explanation:

  • node is a shortcut for the latest stable version of Node.js.

3. Installing a Specific Node.js Version

Code:

# Install Node.js version 16.20.1
nvm install 16.20.1

Explanation:

  • Installs a specific version, ensuring compatibility with projects requiring older versions.

4. Using a Specific Node.js Version

Code:

# Switch to Node.js version 16.20.1
nvm use 16.20.1

Explanation:

  • Activates the specified Node.js version for the current shell session.

5. Listing Installed Node.js Versions

Code:

# List all installed versions of Node.js
nvm list

Output:

->     v18.17.0
       v16.20.1
default -> 16.20.1

Explanation:

  • The -> indicates the active version.
  • default is the version used automatically in new shell sessions.

6. Setting a Default Node.js Version

Code:

# Set Node.js version 18.17.0 as the default
nvm alias default 18.17.0

Explanation:

  • Configures v18.17.0 to be used as the default version whenever a new terminal session starts.

7. Uninstalling a Node.js Version

Code:

# Remove Node.js version 16.20.1
nvm uninstall 16.20.1

Explanation:

  • Frees up space by removing unused Node.js versions.

Advanced Usage

1. Checking Available Versions:

nvm ls-remote

Displays a list of all available Node.js versions.

2. Using nvm in CI/CD Pipelines:

nvm can be used in CI pipelines to test applications with multiple Node.js versions.

Example in a shell script:

nvm install 18
nvm use 18
node app.js

3. Installing LTS Versions:

nvm install --lts

Installs the latest Long-Term Support (LTS) version of Node.js.


Benefits of Using nvm

    1. Version Flexibility:

    • Easily switch between Node.js versions for different projects.

    2. Project Compatibility:

    • Ensure that older projects run with the required Node.js version.

    3. Testing Across Versions:

    • Test applications on multiple Node.js versions without additional setup.

    4. Lightweight Management:

    • Avoid system-wide Node.js installations, keeping the environment clean.

Troubleshooting Tips

    1. Command Not Found Error:

    If nvm commands are not recognized, ensure your shell configuration file (.bashrc, .zshrc, etc.) includes:

     
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
    

    2. Node Version Not Changing:

    • Use nvm use <version> and verify with node -v.
    • Restart the terminal after setting a new default version.

Best Practices

    1. Always install nvm from the official repository for security and reliability.

    2. Use LTS versions for production applications unless a specific feature requires the latest version.

    3. Regularly update nvm to access new features and fixes:

     
    nvm install --lts
    nvm alias default lts
    

Practical Guides to Node.js Snippets and Examples.



Follow us on Facebook and Twitter for latest update.