PM2 command not found - node.js

I installed node.js and npm to my centOS 7 server. But i have problems with pm2.
Actually real problem is i don't have experiences in linux and i don't know how to change path.
Here is folder structure.
* bin
* code
* error_docs
* httpdocs
* lib64
* logs
* tmp
* var
* chat(my node.js folder)
* node_modules
* pm2
* sockjs
* server.js
* dev
* etc
* lib
* local
* sbin
* usr
I entered folder by typing cd chat and installed pm2 with npm install pm2.
After that I tried use pm2 for my server.js by typing pm2 server.js server returns "pm2 command not found". I can use node.js without any problem but pm2 not working.
How can i solve this?

Install PM2 globally:
run as root:
npm i -g pm2
or if user is sudo-er
sudo npm i -g pm2
and then go back to user (or stay in root if it was created by root user) and run it:
pm2 start server.js

PM2 the process manager for Node.js applications. PM2 basically manages applications (run them in the background as a service). So this is how we install PM2 globally with sudo permissions account
sudo npm install -g pm2
The -g option tells npm to install the module globally, so that it's available system-wide.
Once this is installed, check the installed path as:
whereis pm2
pm2: /opt/node/bin/pm2 /opt/node/lib/node_modules/pm2/bin/pm2
Now, we need to add this path in startup bash script. Add add the following line anywhere in ~/.bashrc file.
export PATH=$PATH:/opt/node/lib/node_modules/pm2/bin
Now re-login or source the bash script as follows(so that bash script runs and path is set)
source ~/.bashrc
and now it should run. check the status of pm2
pm2 status

In my case, I have MacOs Big Sur running with zsh shell.
The first thing you need to do is get the prefix of your npm-global path:
npm config get prefix
Then this will be return some thing like this:
/Users/your_user/npm-global
Copy this path, and add the /bin in the end -> /Users/your_user/npm-global/bin. Then we will export this path into the bash configs.
export PATH=$PATH:/Users/your_user/npm-global/bin
I believe all yours global npm packages will work fine now.

Error on using port 80 with PM2?
The wrong way of going about this is trying to run with sudo.
The correct way of doing this would be to login as root sudo su, then run pm2 start app.js --name "whatever" --watch.
Logging in as root, there's no need to configure any bashrc or profile files. However, as root, the script can use nodejs's exec() function dangerously. To avoid this, do the root stuff first with your script, then lower your privilege after some timeout:
// I use port 80 first.. at this point the script's UID is root.
app.listen(80);
// After 2 seconds we switch to UID `azureuser`, which obviously isn't root anymore.
setTimeout(function() {
process.setuid("azureuser");
}, 2000);

If you install through NPM and it does not work, you can create a symbolic link as well :
ln -s /<your-user>/.npm-global/lib/node_modules/pm2/bin/pm2 /usr/bin/pm2
After that, you're going to be able to call:
pm2

Install PM2 globally and run everything as a root user
sudo apt-get install npm
sudo npm i -g pm2
sudo ln -s /usr/bin/nodejs /usr/bin/node
You are good to go

If you used nvm to install node and npm, install pm2 for normal user.
run as root:
sudo su
vim ~/.bashrc
append below code, change NVM_DIR to you normal user's home folder:
export NVM_DIR="/home/[PLEASE CHANGE]/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
# This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \.
"$NVM_DIR/bash_completion"
# This loads nvm bash_completion
at last :
source ~/.bashrc

This option helped me:
sudo npm i -g pm2

yum install npm -y
npm config set strict-ssl false
npm install pm2 -g

sudo npm i -g pm2
It worked for me.

The same as #Henrique Van Klaveren answer but simpler with using substitution:
export PATH=$PATH:$(npm config get prefix)/bin

Related

Run Laravel Mix without a global nodejs and npm installation

I have a laravel project and I need to build styles and scripts with laravel-mix, but a testing server (Ubuntu 20.04.4) hasn't a globally installed node. Node and npm are in different folders in the system so I run commands like this:
/path/to/node /path/to/npm install
/path/to/node /path/to/npm run dev
But when I run npm run dev (this command runs laravel-mix build), I see the error:
> mazer#2.0.0 dev
> mix
/usr/bin/env: ‘node’: No such file or directory
In the package.json it looks like this:
"scripts": {
"dev": "mix"
...
}
I checked the laravel-mix package (in node_modules) and found this: #!/usr/bin/env node. The package checks the node var in this file, but there is no node var.
I don't need to change the env file, so how can I change this path or set a temporary system var? Is there any way to simulate that the variable is there?
I have one solution for this problem.
The issue regarding naming misspelling or path symlinks.
so that you need to link symlinks for nodejs with this command
ln -s /usr/bin/nodejs /usr/bin/node
or
sudo ln -s /usr/bin/nodejs /usr/bin/node
I resolved my issue with Docker, so now I run this command on git push:
docker run --rm -v /path/to/project:/var/www/html node:16.16.0-alpine npm run dev --prefix /var/www/html
Perhaps it will be useful to someone.
UPD
I found another way to resolve it, I use PATH incorrectly and for this reason it didn't work:
Wrong
I set paths to node and npm and then add it to PATH like this:
NODE_PATH="/path/to/node_folder/node"
NPM_PATH="/path/to/node_folder/npm"
PATH="${NODE_PATH}:${NPM_PATH}:$PATH"
And the system can't find npm and node anyway.
The right way
Add /path/to/node_folders (node and npm are in it) to PATH:
NODE_DIR="/path/to/node_folder"
PATH="${NODE_DIR}:$PATH"
And then, I can run just npm install and npm run dev without full paths to them.

Command 'pm2' not found

I recently cloned my nodejs express app on ec2 ubuntu instance. I ran npm install pm2 but it didn't have write permissions. So I ran
sudo chown _R $USER /usr/lib/node_modules
After that PM2 got installed but when I hit pm2 start app.js, it shows pm2 command not found. I have installed it globally locally but nothing works.
What should I do? Check out the screenshot of ERROR
pm2 needs to be installed globally (on the server) to function correctly.
Try
sudo npm install -g pm2
Digital Ocean has an excellent tutorial on this.
in your home directory
sudo npm install -g pm2
relogin to OR restart your instance
There is a simple way to solve it, just add the pm2 statement as a new script in your package.json file.
In your root project folder type
nano package.json
Then the package file opens and you can add the following line after the script line
"pm2 start src/<yourappname.js>"
your file should look like this
"scripts": {
"pm2": "pm2 starts src/<yourappname.js>"
}
Then press ctl + x and enter to save
(You ust replace src/<yourappname.js> for the path adn name of your js file, in my case I have a folder called src into my project root folder)
Lastly, just run the script by typing the following line
npm run pm2
and you got it.

Start node server on machine startup

I was trying to start node.js server on startup of the machine (ubuntu 16.04) with upstart by using the the following code in nodeserv.conf file:
#!upstart
description "Node.js server"
author "Sushant Kumar"
start on started mountall
stop on shutdown
respawn
respawn limit 99 5
script
export APP_HOME = "/home/ubuntu/chatbot_server"
export HOME = "/home/ubuntu"
cd $APP_HOME
exec sudo -u ubuntu /usr/local/nodejs/bin/node $HOME/chatbot_server/server.js >> /var/log/chatbotserv.log 2>&1
end script
post-start script
echo "Node Started"
end script
, but I run the command
# start nodeserv
I get the followig error: >>start: Job failed to start.
Can anyone help me please where I am going wrong?
Edit: This server is hosted on AWS EC2 instance (if that helps, I don't think it's relevant, but just in case).
Have a look on PM2.
PM2 is a really powerful Node.js process manager.
After install your app, you can easily set it on startup with:
sudo systemctl start pm2-yourusername
You can do this by running your app as a service. You can use forever to ensure that a given script runs continuously. First of all you need to install forever. Then go to your project directory and install forever-monitor. Now you can start your app.
npm install forever -g
cd /path/to/your/project
npm install forever-monitor
forever start app.js
Now you need to use forever-service to build your node script as a service.Firstly, install forever-service and then install your app as a service.
npm install -g forever-service
forever-service install test
If you want to work on your script, you can replace this code in your script.
export HOME="/root"
exec /usr/local/nodejs/bin/node /home/ubuntu/chatbot_server/server.js >> /var/log/node.log 2>&1

Launch a Node.Js server when the operating system starts

I have a website written in Node.Js that I launch with this command :
cd /var/mywebsite
npm run dev
What should I put in /etc/init.d/rc.local ?
A solution is : https://causeyourestuck.io/2016/04/30/run-node-js-script-startup/
But I don't know how to create the app.js...
Thanks !
you could use continues integration tools such as puppet to help you do accomplish this. You can also integrate it with Jenkins for the full lifecycle.
https://forge.puppet.com/puppet/nodejs
Given that you are on Linux Ubuntu I think the best and most simple option is a CronTab
#crontab -e
#reboot /home/user/startServer.sh
then your shell/bash script should look something like this
#!/usr/bin/bash
node server.js
or
#!/user/bin/bash
npm run dev
Let me know if that helps, or if you run into issues. I haven't used linux in a few months :)
more info: How to run a shell script at startup
I would recommend a process manager like pm2.
npm i -g pm2
#install pm2
pm2 start <in your project directory>
#starts the project (can start multiple)
pm2 startup
#setup init scripts to start processes on reboot
pm2 save
#saves list of processes
Ref: http://pm2.keymetrics.io/docs/usage/startup/
If you want to use forever-service module, you can use it like this:
Install forever & forever-service packages globally:
sudo npm install -g forever forever-service
Then register your app as a service (default to app.js, but you can specify the name of your app if you need to):
sudo forever-service install your_app --script the_name_of_your_app --start
I have understood the problem and founded the solution !!
A big thank to #Brahma Dev and #TGrif for their help !
The problem was that on the server I have node.js install globally (via apt-get) and an other version installed a the user which launch npm run dev
And if I execute su myuser -c "node -v, su doesn't execute .bashrc before node -v.
So if I execute su myuser and then node -v, the version of node is different !
I have solved the problem by creating an sh script containing :
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
npm run dev
And now, su myuser -c "./start-server.sh" works !
So if I add su myuser -c "/var/www/dialoguea.co.tools/dialoguea/start-server.sh" & in /etc/rc.local the node.js server loads when ubuntu is restarted.

Npm not working after Mavericks update

My npm in terminal isn't working after Mavericks update.
node app.js works well and runs my app, but when I run npm followed by anything I get -bash: npm: command not found. I know this question has been asked before here:
Global installation with npm doesn't work after Mac OS X Mavericks update
and
How do I install a module globally using npm?
and npm not working after reinstalling Mac OS X
, but none of the answers resolved my situation.
$ echo $PATH /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/mongodb/bin
How did you install node.js ? typically it comes bundled with both node and npm, where both are in the same directory. My suggestion is to remove your current install(s) and do the following with just works.
to install nodejs and npm as yourself NOT root do these commands (OSX/linux) :
parent_dir=${HOME}/bin_xxxx # replace bin_xxx with something specific
# to node release like bin_v0.10.31
mkdir ${parent_dir}
download source from : http://nodejs.org/download/
cd node-v0.xxxx
./configure --prefix=${parent_dir}/nodejs
make -j8
make install
which puts it into dir defined by above --prefix
export PATH=${parent_dir}/nodejs/bin:$PATH
define environment variable NODE_PATH so node can find dir for modules otherwise
npm install xxx will put newly installed module into dir in curr dir :
export NODE_PATH=${parent_dir}/nodejs/lib/node_modules
do above AND use syntax : npm install -g some_cool_module
always use the -g for global so it gets installed into dir $NODE_PATH
and not your $PWD
nodejs install gives you npm as well :
ls -la ${parent_dir}/nodejs/bin
For Debian, after installing node do following
curl -k -O -L https://npmjs.org/install.sh
ln -s /usr/bin/nodejs /usr/bin/node
sh install.sh

Resources