Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create duplicate services when using PM2 with next.js #5197

Closed
jim-king-2000 opened this issue Sep 18, 2018 · 6 comments
Closed

Create duplicate services when using PM2 with next.js #5197

jim-king-2000 opened this issue Sep 18, 2018 · 6 comments

Comments

@jim-king-2000
Copy link

jim-king-2000 commented Sep 18, 2018

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.

@JonatanSalas
Copy link

@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!

@JonatanSalas
Copy link

@jim-king-2000 try running in the following way:

pm2 start npm --name "myproj" -- start -i 1

@jim-king-2000
Copy link
Author

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]

@timneutkens
Copy link
Member

Did you run next build before starting the app?

@jim-king-2000
Copy link
Author

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.

@timneutkens
Copy link
Member

🙏 great!

@lock lock bot locked as resolved and limited conversation to collaborators Sep 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants