Skip to content

← Writing

engineering

Resolving "'node': No such file or directory" with nvm

· Jerwin Arnado

If you’ve installed Node.js using nvm (Node Version Manager) and encountered the error:

/usr/bin/env: 'node': No such file or directory

This indicates that the node executable is not accessible in the system PATH. This issue often arises in non-interactive shells or scripts because nvm does not modify the global environment directly. Here’s how you can resolve this problem by creating symbolic links to the node and npm binaries.

To make node and npm accessible globally, even outside of nvm, you can create symbolic links in /usr/local/bin. This approach allows the system to locate the correct binaries regardless of the shell environment.

Run the following commands:

ln -s "$(nvm which default)" /usr/local/bin/node
ln -s "$(nvm which default | sed 's/node/npm/')" /usr/local/bin/npm

What these commands do:

  1. ln -s "$(nvm which default)" /usr/local/bin/node
    • nvm which default returns the path to the node executable of the default version managed by nvm.
    • ln -s creates a symbolic link from the node binary to /usr/local/bin/node.
  2. ln -s "$(nvm which default | sed 's/node/npm/')" /usr/local/bin/npm
    • nvm which default | sed 's/node/npm/' determines the path to the corresponding npm executable.
    • ln -s creates a symbolic link from the npm binary to /usr/local/bin/npm.

Why This Works

The /usr/local/bin directory is usually included in the system PATH. By creating symbolic links in this directory, you make the node and npm commands globally accessible, bypassing the need to load nvm explicitly in every shell session.

Step-by-Step Instructions

  1. Log in to your server:

    ssh your-user@your-server
    
  2. Ensure nvm is installed and configured. Verify the installation:

    command -v nvm
    

    If this returns nothing, install nvm:

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash
    

    Load nvm:

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

    Check installed Node.js versions:

    nvm ls
    

    Set a default Node.js version if one is not already set:

    nvm alias default node
    
  3. Create the symbolic links:

    ln -s "$(nvm which default)" /usr/local/bin/node
    ln -s "$(nvm which default | sed 's/node/npm/')" /usr/local/bin/npm
    
  4. Verify the setup:

    node --version
    npm --version
    

    If both commands return their respective version numbers, the issue is resolved.

When to Use This Solution

This solution is ideal if:

  • You frequently run scripts in non-interactive environments (e.g., cron jobs, CI pipelines, or SSH commands).
  • You want to avoid sourcing nvm in every shell session manually.
  • You prefer a global node and npm setup while still using nvm for version management.

Caveats and Best Practices

  • Switching versions: if you change the default Node.js version in nvm, you must recreate the symbolic links to point to the new version.
  • Conflicts: this approach exposes node and npm globally, which might conflict with system-installed versions (if any).
  • Permissions: you need sufficient permissions to create symbolic links in /usr/local/bin. Use sudo if required.
  • Undoing: to remove the symbolic links, simply run:

    rm /usr/local/bin/node
    rm /usr/local/bin/npm
    
  • Alternative: if you’d rather not use symbolic links, ensure nvm is sourced in all shell sessions by adding the following to your ~/.bashrc:

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

Conclusion

By creating symbolic links to the node and npm binaries managed by nvm, you can resolve the “/usr/bin/env: ‘node’: No such file or directory” error. This approach ensures these tools are globally accessible, simplifying your development and deployment workflows.