pm2 not running next.js – Next.js

by
Ali Hasan
aws-aurora-serverless next.js node.js pm2 reactjs

The Problem:

A user is trying to use pm2 to keep their Next.js application running on an apache server, but pm2 is not starting the application. The user is getting an error message that says "no process found". The user is not sure what the problem is and is asking for help.

The Solutions:

Solution 1: Run pm2 with sudo permissions as non-root user

To start pm2 as a non-root user, you can use the following steps:

  1. First, run sudo su to switch to the root user.
  2. Then, install pm2 globally using sudo npm install pm2 -g.
  3. Once pm2 is installed, you can start your Next.js application using pm2 start npm --name "khulo" -- start.

This should start your Next.js application and keep it running even if you close your terminal window.

Solution 1: Fix the PM2 Command

The provided PM2 command has a syntax error. The correct syntax is:

pm2 start "npm run start" --name "khulo"

Ensure that the command is entered correctly, with the quotes enclosing the entire npm run start command.

Q&A

Nginx running with Nextjs app works fine with ‘npm run start’, but as soon as the terminal is closed, it stops. How to keep it running?

Consider using pm2 to keep the app running even after the terminal is closed.

How to run pm2 in this case?

Use the following command: pm2 start "npm run start" --name "khulo"

What privileges are required to use pm2?

Superuser privileges (sudo) are required to use pm2. For non-root users, refer to the pm2 documentation.

Is there a way to start pm2 without sudo?

Yes, try: pm2 start "npm run start" --name "khulo"

Video Explanation:

The following video, titled "How to Deploy a Next.js app to a Custom Server - NOT Vercel! (Full ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

... Next.js App 20:35 Step 6 | Implementing PM2 23:47 Bonus Step | Modifying the Site 28:04 Hostinger VPS Benefits 28:42 Outro Let's get it ...