简体   繁体   中英

How to set port for Next js on PM2

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?

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM