How to run nuxt under pm2?

For those who just want to get the app started in Production mode:

Seeing this page comes up at the top of search results of how to use Nuxt.js with PM2, I thought I should add this answer.

All you need to add to your universal Nuxt app for serving it though PM2 is a file called ecosystem.config.js. Create a new file with that name in your root project directory and add the following content:

module.exports = {
  apps: [
    {
      name: 'NuxtAppName',
      exec_mode: 'cluster', // Optional: If you want it run multiple instances.
      instances: 'max', // Or a number of instances.
      // 'max' auto detects how many CPU cores there are.
      // The previous option must exist to use the above.
      script: './node_modules/nuxt/bin/nuxt.js',
      args: 'start',
    },
  ],
}

Now build your app with npm run build.

And serve it with pm2 start.

Check the status pm2 ls.

Your Nuxt.js application is now serving!

Source


The code below:

pm2 start npm --name "anyName" -- run dev

While JC97 s answer is great, the Nuxt has upgraded, also if you want to run debug, development, staging and production setup separately you need a more compartmentalized ecosystem file

First install nuxt as npm install --save nuxt

Make a folder called config inside your project folder, this folder is a sibling to the server pages middleware and other directories that nuxt generates

Add 4 .env files .env.deb, .env.dev, .env.sta, .env.pro for debug, development staging and production

Run the command pm2 ecosystem which will generate the default file

Replace the default file with the one below

It creates each environment as a separate app

If you want to run only the staging version you would run it as pm2 start ecosystem.config.js --only myapp_sta

You can run all 4 at time if you WANT!!!

const dotenv = require('dotenv')

const autorestart = true
const watch = false
const maxMemoryRestart = '512M'

module.exports = {
  apps: [
    {
      name: 'myapp_dev',
      script: 'npm run clear && npm run dev',
      instances: 1,
      autorestart,
      watch,
      max_memory_restart: maxMemoryRestart,
      env: dotenv.config({ path: './config/.env.dev' }).parsed
    },
    {
      name: 'myapp_deb',
      script: 'npm run clear && npm run deb',
      instances: 1,
      autorestart,
      watch,
      max_memory_restart: maxMemoryRestart,
      env: dotenv.config({ path: './config/.env.deb' }).parsed
    },
    {
      name: 'myapp_sta',
      script: 'npm run clear && npm run sta',
      instances: 1,
      autorestart,
      watch,
      max_memory_restart: maxMemoryRestart,
      env: dotenv.config({ path: './config/.env.sta' }).parsed
    },
    {
      name: 'myapp_pro',
      script: 'npm run clear && npm run build && npm run start',
      instances: 1,
      autorestart,
      watch,
      max_memory_restart: maxMemoryRestart,
      env: dotenv.config({ path: './config/.env.pro' }).parsed
    }
  ],

  deploy: {
    myapp_dev: {
      user: 'zupstock',
      host: '192.168.1.103',
      ref: 'origin/master',
      repo: '[email protected]:owner/myapp_v1.git',
      path: '/',
      'post-deploy':
        'cd myapp_v1 && npm install && pm2 startOrRestart ecosystem.config.js --only myapp_dev'
    }
  }
}

For multiple nuxt instances to work with pm2 you need to point to the nuxt-start script in node_modules/nuxt/bin/nuxt-start.

This guide explains it well. To summarize: You need an ecosystem configuration for your apps (ecosystem.config.js) where you set all the given parameters for your app. Here's a list with all the available parameters.

Your's should look like this:

module.exports = {
  apps: [
    {
      name: 'app1',
      port: 3000,
      script: './node_modules/nuxt/bin/nuxt-start',
      cwd: '/home/user/your-nuxt-project/app1',
      env: {
        NODE_ENV: 'development'
      },
      env_production: {
        NODE_ENV: 'production'
      }
    },
    {
      name: 'app2',
      port: 4000,
      script: './node_modules/nuxt/bin/nuxt-start',
      cwd: '/home/user/your-nuxt-project/app2',
      env: {
        NODE_ENV: 'development'
      },
      env_production: {
        NODE_ENV: 'production'
      }
    }
  ]
};

Then simply cd to your project dir and run sudo pm2 start. It will automatically find the config file and run both apps simultaneously.