I’m having trouble with a global npm installation on my Ubuntu 18.04 system. When I run npm install -g lerna the installation completes without any errors. However, when I try to use the installed package by running lerna --version, I get an error saying “lerna:command not found”.
I think the issue might be related to where the package is being installed. The package gets installed to /home/surajkulriya/.npm-global/lib/node_modules instead of /usr/local/lib/node_modules where my other working npm packages are located.
Here are my current paths:
which npm: /usr/local/bin/npm
which node: /usr/local/bin/node
which nodejs: /usr/bin/nodejs
How can I fix this PATH issue so that globally installed npm packages work properly? Is there a way to change where npm installs global packages or update my PATH configuration?
This issue typically arises when npm uses a non-standard prefix for global installations. To check if lerna is correctly installed, you can run npm list -g --depth=0. Conflicting configurations often occur when nodejs is installed via the apt package manager, while npm is installed independently.
Instead of altering the PATH or prefix settings, you could leverage npx, which allows you to run packages without needing them in your PATH: npx lerna --version. For a more lasting fix, consider using Node Version Manager (nvm) to manage your Node.js installation. This helps resolve conflicts between system-installed nodejs and independently installed npm and manages PATH configuration automatically, ensuring global packages work seamlessly.
check if ~/.npm-global/bin exists and has the lerna executable in it first. sometimes the symlink doesn’t get created properly during install. if its there then yeah just update your PATH like lucask mentioned, but if not try reinstalling lerna with the --force flag
Had the exact same issue on my Ubuntu setup a while back. The problem is that npm is using a custom global directory that isn’t in your PATH. You can verify this by running npm config get prefix - it’s probably showing /home/surajkulriya/.npm-global instead of /usr/local. The quickest fix is to add the bin directory to your PATH. Open your .bashrc or .profile file and add this line: export PATH="$HOME/.npm-global/bin:$PATH". Then either restart your terminal or run source ~/.bashrc to reload the configuration. Alternatively, you can change npm’s global directory back to the system default by running npm config set prefix /usr/local, but you might need sudo permissions for future global installs. I personally prefer the first approach since it keeps user-installed packages separate from system packages and avoids permission headaches.