Changing Node.js Version Using nvm

Faizan HabibFaizan Habib
2 min read

If you've already installed any Node.js version eg. 20 using nvm (Node Version Manager), you can still use nvm to switch to any other version, including 18.16.0. Here's how you can do it:

1. Installing nvm (Node Version Manager):

If you don't have nvm installed, follow these steps to install it:

  • Visit the GitHub repository: nvm-sh/nvm

  • Follow the installation instructions provided in the repository's README file.

2. Switching to Node.js Version 18.16.0:

Once nvm is installed, follow these steps to switch to Node.js version 18.16.0:

  • Open your terminal.

  • Use the following command to list all installed Node.js versions:

      nvm ls
    
  • If Node.js version 18.16.0 is not already installed, install it using the following command:

      nvm install 18.16.0
    
  • Once installed, switch to version 18.16.0 using:

      nvm use 18.16.0
    
  • Verify the version switch by running:

      node -v
    

    This command should display the Node.js version as 18.16.0.

  • Optionally, set Node.js version 18.16.0 as the default version:

      nvm alias default 18.16.0
    

    This will set Node.js version 18.16.0 as the default version for new terminal sessions.

Uninstalling All Dependencies Listed in package.json (Optional):

If you want to remove all dependencies listed in your project's package.json file, you can use the following npm command:

npm uninstall *

This command removes all local packages listed in the package.json file.

Alternatively, you can use the following command, which provides a more explicit approach:

npm uninstall `ls -1 node_modules | tr '/\n' ' '`

This command lists all the packages installed in the node_modules directory and uninstalls them.

Note: Ensure you are in the project directory when running these commands, and always review the actions before confirming to avoid unintended consequences.


3
Subscribe to my newsletter

Read articles from Faizan Habib directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Faizan Habib
Faizan Habib