How to set port for Next js on PM2
Asked Answered
F

2

6

i have an ordinary next js application with the following scripts in my package.json file.

  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start -p 3002"
  },

I have a start script with the -p tag to start the application on port 3002.

I hope to run this application using pm2 with the following script.

PORT=3002 NODE_PORT=3002 pm2 start npm --name "nextjs-website" -- start

Using the env variables PORT and NODE_PORT don't work. How do i start this application using the same port as mentioned in the package.json file.

Also is it possible to run the pm2 service the run the start in the package.json file?

Furrier answered 2/11, 2020 at 11:41 Comment(0)
H
12

In package.json add this start script "start": "next start -p 8000" and then use PM2 to start it with

pm2 start yarn --name "nextjs" --interpreter bash -- start
pm2 show nextjs

check this article for more details. https://www.willandskill.se/en/setup-a-next-js-project-with-pm2-nginx-and-yarn-on-ubuntu-18-04/

Hollington answered 2/11, 2020 at 11:52 Comment(0)
G
20

Setting port number in npm script is not good idea at all.

Use this command to start server at any port

sudo pm2 start npm --name "dev-staging" -- run start -- --port=8000

or use an alias

sudo pm2 start npm --name "dev-staging" -- run start -- -p 8000

Garble answered 9/6, 2022 at 16:35 Comment(0)
H
12

In package.json add this start script "start": "next start -p 8000" and then use PM2 to start it with

pm2 start yarn --name "nextjs" --interpreter bash -- start
pm2 show nextjs

check this article for more details. https://www.willandskill.se/en/setup-a-next-js-project-with-pm2-nginx-and-yarn-on-ubuntu-18-04/

Hollington answered 2/11, 2020 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.