Resolving Node and npm Path Issues with NVM on Ubuntu
Jun
26

Resolving Node and npm Path Issues with NVM on Ubuntu

As a developer, I recently encountered an issue on my Ubuntu server where the globally installed pm2 command was not being recognized after an SSH session. Despite installing pm2 multiple times using the -g flag, I consistently received the error command pm2 not found. Upon further investigation, I discovered that the root cause was related to the NVM (Node Version Manager).

The issue arose due to a specific sequence of Node.js installations: I initially installed Node.js version 20, later switched to version 16, installed pm2 under version 16, and then uninstalled Node.js version 20. This sequence disrupted the environment configuration managed by NVM, leading to inconsistencies in command availability.

l Here's a detailed of the problem and the steps I took to resolve it.

Problem Overview

I had installed pm2 globally using the following command:

npm install -g pm2

 

 

 

However, when I SSH'd into the server and tried to run the pm2 command, I was met with:

command pm2 not found

Additionally, running node -v or npm -v also resulted in command not found errors, indicating a problem with NVM not setting up the environment correctly.

Diagnosing the Issue

  1. Verify NVM Installation:
    I first checked if NVM was installed and recognized by running:
    command -v nvm

    This command should return the path to the NVM executable if it is installed correctly.

  2. Check Node.js and npm Availability:

    Since NVM was not properly setting up Node.js and npm, I ran:

    node -v
    npm -v

    Both commands returned command not found, confirming the issue.

     

Solution Steps

To resolve the issue, I followed these steps:

  1. Source the NVM Script: I ensured that the NVM initialization script was sourced in my shell configuration file. For my setup, I used .bashrc, but it could also be .bash_profile or .zshrc depending on your shell:

    echo 'source ~/.nvm/nvm.sh' >> ~/.bashrc
    source ~/.bashrc
  2. Verify NVM Functionality:
    After sourcing the script, I verified that NVM was functioning correctly:
    nvm --version
  3. Install Node.js:
    If Node.js was not installed via NVM, I installed it using:
    nvm install node
  4. Use the Installed Node.js Version:
    I set the installed Node.js version to be used in the current session:
    nvm use node
  5. Set the Default Node.js Version:
    To ensure that the correct Node.js version is used in all future terminal sessions, I set it as the default:
    nvm alias default node
  6. Verify Installation of Global npm Packages:
    Finally, I checked if pm2 was globally installed and accessible:
    npm list -g pm2
    If pm2 was not listed, I reinstalled it:
    npm install -g pm2

    Conclusion

    By correctly setting up NVM and ensuring the Node.js and npm paths were properly configured, I was able to resolve the issue of the pm2 command not being found. These steps ensured that the Node.js environment was consistently available across all SSH sessions, preventing similar issues in the future. This experience underscores the importance of proper environment configuration, especially when using tools like NVM to manage multiple Node.js versions.

Contact

Get in touch with us

Feel free to request missing tools or give some feedback.

Contact Us