Reputation: 3886
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?
Upvotes: 6
Views: 21250
Reputation: 1094
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
Upvotes: 21
Reputation: 312
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/
Upvotes: 12