Purple Pig
Purple Pig

Reputation: 13

PM2 How to start nextjs project in prod mode?

I want to run a project in pm2.

My folder structure:

folder

My package.json:

"scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start -p 7777",
    "type-check": "tsc",
    "gensitemap": "node generate-sitemap.mjs",
    "prod": "yarn && npm run build && npm run start"
  },

My ecosystem.config.js

module.exports = {
  apps: [
    {
      name: 'core',
      script: 'yarn --cwd core prod',
      watch: false,
    },
  ],
};

When, I started pm2 start ecosystem.config.js
I got the following error in the logs
error - Invalid project directory provided, no such directory: D:\React\MyProject\src\core\prod

I tried different ways to run in production mode. It came out only in dev mode to run

Upvotes: 1

Views: 2280

Answers (2)

Bekzod
Bekzod

Reputation: 131

With PM2 it isn't important to use ecosystem.config.js. Just add what command do you want in package.json file like below:

"scripts": {
    "dev": "next",
    "prod": "yarn && npm run build && npm run start"
     .
     .
     .   
  
  }

Then run pm2 command:

DEV

pm2 start "pm2 run dev" --name YOUR_APP_NAME -- start

PROD

pm2 start "pm2 run prod" --name YOUR_APP_NAME -- start

Upvotes: 1

박제영
박제영

Reputation: 109

if you are using nextjs, try use the code below. script is changed

module.exports = {
  apps: [
    {
      name: 'core',
      script: "node_modules/next/dist/bin/next",
      watch: false,
    },
  ],
};

Upvotes: -1

Related Questions