In the projects not using next.js, we use "pm2 start pm2.json. It never generates duplicate pm2 instances.
When using PM2 with next.js, we can't use pm2.json configuration file. The code is like the following:
// package.json
"start": "node --experimental-modules server.mjs"
// deploy.sh
pm2 start npm --name "myproj" -- start
When I run deploy.sh without deleting the existing "myproj" pm2 instance, there would be two "myproj" instances. And it disables the auto-deployment script.
Looking forward to the answer.
@jim-king-2000 would you mind to explain it a little?
I'm using nextjs v7 with PM2, and I use the javascript configuration file:
It looks like this:
//pm2.config.js
module.exports = {
apps: [
{
instances: 1,
max_restarts: 10,
merge_logs: false,
script: './index.js',
max_memory_restart: '200M'
}
]
};
We also deploy our app using a Docker image. We run this file using pm2 start pm2.config.js and there's no problem. Maybe you need to specify when running pm2 the way you run the number of instances, in order to force instance regeneration and removal of the current running instance?
Hope this helps!
@jim-king-2000 try running in the following way:
pm2 start npm --name "myproj" -- start -i 1
I use pm2.json like the following:
{
"apps": [
{
"name": "fe-fireeye",
"script": "./pm2Server.js",
"exec_mode": "fork",
"instances": 1,
"node_args" : "-r esm",
"env": {
"NODE_ENV": "production",
"NODE_PORT" : "8090"
},
"max-memory-restart": "300M"
}
]
}
Then I type pm2 start pm2.json, it compains:
0|fe-fireeye | > Could not find a valid build in the 'D:\projects\location-service-fireeye\fe-fireeye.next' directory! Try building your app with 'next build' before starting the server.
PM2 | App [fe-fireeye] with id [0] and pid [4024], exited with code [1] via signal [SIGINT]
Did you run next build before starting the app?
It works. I'm so sorry. The error message is the old one. I see the old error and do not try to access the server. I close the thread. It is a false alarm.
馃檹 great!
Most helpful comment
Did you run
next buildbefore starting the app?