Does Next.js support pm2 cluster mode? #10675
-
Hi guys, My pm2 version is 4.2.3, and express version is 4.17.1. next and related version is below. // pages/index.js
export default function Index() {
return (<div><p>Hello Next.js</p></div>)
} // package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"dependencies": {
"next": "^9.2.2",
"react": "^16.12.0",
"react-dom": "^16.12.0"
},
"devDependencies": {},
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"author": "",
"license": "ISC"
} when I run the next server // test.js (express server)
const express = require('express')
const app = express()
const uuid = require('uuid')
var instance_id = uuid.v4()
const port = 3000
app.get('/', function (req, res) {
res.send(`Hello World! ${instance_id}`)
})
app.listen(port, function () {
console.log(`application is listening on port ${port}...`)
}) I posted this on spectrum, too. |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 32 replies
-
I had similar problem (not with pm2, but the error was the same) and the issue in my case was that the version of Node was outdated. Can check what version of Node are you using and update to more recent one? |
Beta Was this translation helpful? Give feedback.
-
Did you check the logs outlined there, it shows only 15 lines which is not enough to know why the command failed to boot. Looks like it should be located here: |
Beta Was this translation helpful? Give feedback.
-
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start",
"pm2": "pm2"
}
{
"apps": [
{
"name": "my-app",
"script": "npm",
"args" : "start",
"instances": "max",
"exec_mode": "cluster"
}
]
}
The configuration above will start as many instances as possible and far as I tried works correctly. I would highly recommend looking into hosting on https://zeit.co/ though it's automatically optimized for Next.js in many ways: https://nextjs.org/docs/deployment#optimized-for-nextjs |
Beta Was this translation helpful? Give feedback.
-
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"pm2": "pm2-runtime start pm2.config.js --env production"
} when i run
but, if I replace pm2 script to The pm2.config.js is: module.exports = {
apps : [
{
name: 'next',
script: 'npm',
args: 'start',
instances: 4,
exec_mode: 'cluster'
}
]
}; |
Beta Was this translation helpful? Give feedback.
-
I had nothing but trouble trying to use I did not have a problem where I had to run This is what is working for me:
I can now run Success. |
Beta Was this translation helpful? Give feedback.
-
When Next.js is run in a cluster, does every process then have its own copy of things such as statically generated files, optimized images (next/image), etc.? Or will every process share these? |
Beta Was this translation helpful? Give feedback.
-
Does anyone know how we can run it in cluster mode inside docker? I have tried all the steps mentioned here but nothing works inside docker container. My Docker command looks like this : CMD [ "pm2-runtime" ,"start","pm2.json"] pm2.json file
Error: (As you can see its looking for .next folder inside pm2.json folder)
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
I solved this issue a while ago. My module.exports = {
apps: [
{
name: 'app-name',
script: "node_modules/next/dist/bin/next",
args: "start",
instances: 4,
exec_mode: 'cluster',
autorestart: true,
},
],
}; Afterward, I deleted all PM2 processes using |
Beta Was this translation helpful? Give feedback.
-
Is anyone facing an issue getting console.log to show up in pm2 logs with this method? Perhaps using a custom express server? |
Beta Was this translation helpful? Give feedback.
pm2
in the dependenciesscripts
section like this:pm2.json
and add the following configuration:npm run build
to build the Next.js app for productionnpm run pm2 start pm2.json
The configuration above will start as many instances as possible and fa…