20

I have an AWS EC2 that is just for keeping my Next.js client side running (there is no backend for it right now). Currently if I go into the terminal and enter npm run develop, the site runs perfectly. However, I want this process to always be running in the background of the instance. Is there a way I can do that with pm2?

I have it installed globally, but it won't let me run pm2 start npm run develop. BTW yes I want it running in development mode right now.

optionalfigure
  • 269
  • 1
  • 2
  • 9
  • `pm2 start npm --name "next" -- run develop` should work. Notice the `--` before `run dev` to pass arguments to the underlying process. – brc-dd Dec 14 '21 at 18:21

6 Answers6

43

yes, but make sure the command exists in the package.json

  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  }

so in this case, the pm2 command will be

pm2 start npm --  dev
#in your case
pm2 start npm --  develop

enter image description here

Or to have a nice name as mentioned by @brc

pm2 start npm --name "next-js" -- dev

enter image description here

While running on EC2, it also good practice to save process for startup

PM2 can generate startup scripts and configure them in order to keep your process list intact across expected or unexpected machine restarts.

https://pm2.keymetrics.io/docs/usage/startup/

pm2 startup
pm2 save

Adiii
  • 54,482
  • 7
  • 145
  • 148
8

Adding to best answer: You should really consider using:

pm2 start npm --  start

as it will run production build, not development (I know OP asked question with develop, but it is important feature considering app being in production.

vortur
  • 91
  • 1
  • 1
2

In the case of using yarn alongside nextjs and sort out a specific port:

pm2 start yarn -- start --port 5500
AmerllicA
  • 29,059
  • 15
  • 130
  • 154
1

Yes you can

 pm2 start npm --name name_of_the_app -- start --port your_port

e.g

pm2 start npm --name abhimanyu -- start --port 3000

Here my app name is (abhimanyu) which is running on port 3000

Note: If you do not provide the port number then it will use the default port which is specified on your project

To get the list of the project which is running in pm2 type

pm2 list

enter image description here

0

Another simple fast approach would be

pm2 start "npm run dev" --name AppName

where in your case it would be,

pm2 start "npm run develop" --name AppName

Note AppName is the name to call your app for easy identification in pm2

-1

I have adde full working step to deploy nextjs app on ubuntu platform using pm2 and nginx.

https://anuragdeep.com/deploy-nextjs-strapi-on-ubuntu-with-nginx-and-pm2/

Anurag Deep
  • 49
  • 1
  • 9