How to start pm2 with arguments? - node.js

I have a simple command that works fine when run:
parse-dashboard --config /home/ubuntu/dash/config.json
However, when running it with pm2, it doesn't work:
pm2 start parse-dashboard -- --config=/home/ubuntu/dash/config.json
looking at the logs, i get the error:
node: bad option: --config=/home/ubuntu/dash/config.json
What am I doing wrong?

Use a process file where you specify the arguments. Create the following file and name it for example ecosystem.json (make sure the 'script' and 'cwd' (where the app will be launched) locations are correct for you))
{
"apps" : [{
"name" : "parse-dashboard-wrapper",
"script" : "/usr/bin/parse-dashboard",
"watch" : true,
"cwd" : "/home/parse/parse-dashboard",
"args" : "--config /home/ubuntu/dash/config.json"
}]
}
And run it with
pm2 start ecosystem.json

Related

PM2 ecosystem.config.js ignore_watch restarting

Running my app with pm2 start index.js --watch functions as expected. I would like to use the config to ignore_watch.
ecosystem.config.js
module.exports = {
apps : [{
name : "appname",
script : "./index.js",
watch : true,
max_mamory_restart : "1G",
ignore_watch : "\.bash(rc|_(history|logout)),\.(cache|config|gitignore|lesshst|local|npm|pm2|profile|sudo_as_admin_successful|vscode_server),.*swp,snap,.*config.js,package-lock.json,node_modules,jira,logs"
}]
}
When I run pm2 start ecosystem.config.js, my app runs, but constantly restarts with Change detected on path .pm2/.......
I thought that maybe I am not writing the ignore_watch correctly, so I tried
ignore_watch : [
".pm2",
"\.bash(rc|_(history|logout))",
"\.cache|config|gitignore|lesshst|local|npm|pm2|profile|sudo_as_admin_successful|vscode_server)",
".*swp",
"snap",
".*config.js,package-lock.json,node_modules,jira,logs"
]
which then gives me constant restarts with Change detected on path .vscode_server/.......
I then tried the same thing by editing ignore_watch to:
ignore_watch : [
".vscode_server",
".pm2",
but this still restarts with with Change detected on path .vscode_server/.......
I don't understand how the ignore_watch is working, especially where the original pm2 start index.js --watch doesn't appear to watch these folders and its not constantly restarting.

How to add node run option --max-http-header-size and add name to pm2 start command

How would I start a pm2 process with the —max-http-header-size node option, as well as name the process.
I have a server with multiple micro-services, one of the services is a web scraper.
This web scraper accepts requests with headers over the nodejs default 8kb limit. So, to run my app locally have to add the --max-http-header-size node option.
I've cloned my app to the server, but don't know how to set --max-http-header-size, nor do I know how to name the process within the pm2 start command.
So far my attempts have looked like this.
// this sets the name, but I don't know how to add the option `--max-http-header-size`
pm2 start npm --name "{REPONAME}" -- start
pm2 start node --name "scraper" --max-http-header-size 15000 app.js
pm2 start node --max-http-header-size 15000 app.js -- --name "scraper"
The accepted answer by David Harvey did not work for me as of Node 16. Instead, I had to use node_args, something like this:
{
"apps" : [{
"name" : "myapp",
"script" : "./app.js",
"node_args": "--max-http-header-size=256000",
"env": {
"NODE_ENV": "production",
}
}]
}
What you're looking for is called environment variables! You can pass environment variables to pm2 using a file that loads your server like this:
module.exports = {
apps : [
{
name: "myapp",
script: "./app.js",
watch: true,
node_args: "--max-http-header-size=16000"
}
]
}
Here's more about it too:
https://pm2.keymetrics.io/docs/usage/environment/

PM2 Error: Cannot find module when using ecosystem file

I'm having an issue with pm2.
If I start my app with
cd /my/path
pm2 start server.js
everything works correctly, but if I use an ecosystem.config.js file, then it gives the following error
pm2 start ecosystem.config.js
Error: Cannot find module '/my/path/server.js'\n at Function.Module._resolveFilename
my ecosystem file is configured as follows
module.exports = {
"apps": [{
"name": "MyApp",
"script": "server.js",
"cwd": "/my/path"
}]
}
I tried uninstalling pm2, updating npm and reinstalling node_modules, but still I can't understand why it says a module is missing in my file (expecially because it works when not using the ecosystem file)
Ensure that the ecosystem.config.js is in the root of your project as well as your server entry file (server.js) and specify your script as below:
module.exports = {
"apps": [{
"name": "MyApp",
"script": "./server.js"
}]
}
Reading from https://pm2.keymetrics.io/docs/usage/application-declaration/
Field Type Example Description
name (string) “my-api” application name (default to script filename without extension)
script (string) ”./api/app.js” script path relative to pm2 start
cwd (string) “/var/www/” the directory from which your app will be launched
I would try:
with "script": "./server.js" (kind of strange, I know)
remove the cwd parameter and setting ./my/path/server.js
Try this command: pm2 start src/server.js
or other folder name before server file name

Unable to start PM2 application with json file

I am facing a peculiar issue. I am trying to start my application with pm2 . Without the json file , it works perfectly fine , but when I add a json file and try to start it , it does not hit my application in the first start , but works well if I stop and start the instance again.Also , when the instance is first started if I do pm2 list , the PID number keeps changing continually. Any help on this please?
PM2 json file :
{
"apps" : [{
"name" : "ABC",
"script" : "./bin/www",
"watch" : true,
"env": {
"NODE_ENV": "development",
"PORT":8443
},
"env_production" : {
"NODE_ENV": "production",
"PORT":4000
},
"error_file" : "err.log",
"out_file" : "out.log",
"merge_logs" : false
}]
}
Command to start the application with pm2 json file : pm2 start sample.json
Command to stop the application with pm2 json file : pm2 stop ABC
to kill : pm2 kill
Thanks in advance.
You might try setting your exec_mode to fork. If I had to guess, it's starting up with a default mode of cluster and it spins up the number of instances equivalent to the number of processors as detected by the system. While this is typically fine for webapps, it's not always suitable for all situations.
You could also try setting instances to 1 in cluster mode
Your PM2-JSON-File looks good but how looks like your
package.json
....
"scripts": {
"start": "node start -p 8443 /bin/www"
"test": "echo \"Error: no test specified\" && exit 1"
},
.....

pm2 with systemd and passing node argument

I want to start node with pm2 and environment variables like --nouse-idle-notification or --max-old-space-size=2048.
However, whatever I do, it is not passing the node variables. I start my app with pm2 and a configuration file. The configuration file looks like:
{
"apps" : [{
"env": {
"NODE_PATH": "/usr/bin/node",
"interpreter_args": "--max-old-space-size=2048 --nouse-idle-notification"
},
"env_development": {
"NODE_ENV": "development"
},
"env_production" : {
"NODE_ENV": "production",
"APP_TYPE": "web"
},
"exec_mode" : "fork",
"name" : "MyApp",
"script" : "/opt/myapp/app.js",
"watch" : false,
"out_file" : "/var/log/app.log",
"error_file" : "/var/log/app.log",
"combine_logs": true,
"node_args": "--max-old-space-size=2048 --nouse-idle-notification",
"args": "--max-old-space-size=2048 --nouse-idle-notification"
}]
}
(as you can see I try to pass in the node variables in multiple ways)
I then start the app with:
pm2 restart pathtojsonfile --env production
Everything is starting up properly and I see variables like "MY_APP" in my code. However, now when I look at the process with "top" I only see:
node /opt/myapp/app.js
When I start the app with forever or manually I can see the process like:
node --max-old-space-size=2048 --nouse-idle-notification /opt/myapp/app.js
Is pm2 just not showing those node arguments or are they really not passed in? (The pm2 started process uses less memory)
Below is extact command to start pm2 with node-args
pm2 start app.js --node-args="--max-old-space-size=4096"
By using "node_args": "--max-old-space-size=2048 --nouse-idle-notification" you did the right thing and these arguments are taken into account.
PM2 renames the process and drop the specified node argument in the process title.

Resources