Issue
I have installed the pm2 using the command below (in root):
npm install pm2@latest -g
When checking the version of pm2 or initialising the instance, I am getting the following error -bash: pm2: command not found
is there any way to fix this issue?
Solution
Try checking if /usr/bin/pm2 exists.
ls -l /usr/bin/pm2
Try checking where npm -g installs things.
npm prefix -g
npm prefix
Try checking what is your $PATH.
echo $PATH
(1) For ls -l /usr/bin/pm2 I am getting "ls: cannot access '/usr/bin/pm2': No such file or directory" (2) For "npm prefix -g", I am getting "/root/.npm-global" (3) For 'npm prefix", I am getting "/root" (4) For "echo $PATH", I am getting "/root/.nvm/versions/node/v18.18.0/bin:/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin". What should I do to solve the issue? Can you update the answer on how to resolve the issue, thanks.
Looks like the problem is that your npm is configured to install into /root/.npm-global, but this directory is not in $PATH.
How did you install nodejs?
I would guess that someone tried to follow the recipe from here: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally but they didn't finish it????
You have four options:
Add /root/.npm-global to $PATH. For example by adding a line like
PATH=$HOME/.npm-global:$PATH
to /root/.profile or /root/.bash_profile or /root/.bashrc. It should take effect on next login.Run pm2 explicitly with its full path
/root/.npm-global/bin/pm2
instead of justpm2
.Remove the npm configuration that tells it to use /root/.npm-global. It should go back to installing to /usr/bin like normal. There is probably a npmrc file somewhere (maybe /root/.npmrc or /etc/npmrc) (documentation) which contains this configuration value. If you don't need this value for some other reason, edit the file and remove that line. If it is the only line, you can remove the whole file. But I don't understand is why it was configured this way, and whether it had a good reason.
For a one time workaround, you can try
npm install -g pm2 --prefix /usr
. Of course you'll have to remember you need to do it the next time you install some other package.
Answered By - Tomi Answer Checked By - Pedro (WPSolving Volunteer)