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.
The Solution: Creating Symbolic Links
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:
ln -s "$(nvm which default)" /usr/local/bin/nodenvm which defaultreturns the path to thenodeexecutable of the default version managed bynvm.ln -screates a symbolic link from thenodebinary to/usr/local/bin/node.
ln -s "$(nvm which default | sed 's/node/npm/')" /usr/local/bin/npmnvm which default | sed 's/node/npm/'determines the path to the correspondingnpmexecutable.ln -screates a symbolic link from thenpmbinary 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
-
Log in to your server:
ssh your-user@your-server -
Ensure
nvmis installed and configured. Verify the installation:command -v nvmIf this returns nothing, install
nvm:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bashLoad
nvm:export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"Check installed Node.js versions:
nvm lsSet a default Node.js version if one is not already set:
nvm alias default node -
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 -
Verify the setup:
node --version npm --versionIf 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
nvmin every shell session manually. - You prefer a global
nodeandnpmsetup while still usingnvmfor 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
nodeandnpmglobally, which might conflict with system-installed versions (if any). - Permissions: you need sufficient permissions to create symbolic links in
/usr/local/bin. Usesudoif 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
nvmis 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.