How to set port for Next js on PM2

Issue

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?

Solution

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/

Answered By – Madhav Palshikar

Answer Checked By – David Goodson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.