How to run nuxt under pm2?
Asked Answered
S

7

14

I have 2 nuxt projects that need to be run on the server. Whenever I run the app locally it seems to be working with:npm run dev, but on the server this needs to be ran under a subprocess, so I use pm2 for that. But whenever I start running the same npm script with pm2 the process gets errored.

The command used for this is: sudo pm2 start npm --name "dev" -- dev, even when I run the apps separately it gets errored. sudo pm2 start npm --name "app1" -- app1:dev and sudo pm2 start npm --name "app2" -- app2:dev


package.json

{
    ...
    "scripts": {
        "app1:dev": "nuxt --config-file src/app1/nuxt.config.js -p=3000",
        "app2:dev": "nuxt --config-file src/app2/nuxt.config.js -p=4000",
        "dev": "concurrently \"npm run app1:dev\" \"npm run app2:dev\"",
    },
    "dependencies": {
        ...
    },
    "devDependencies": {
        "concurrently": "^3.6.0",
        "cross-env": "^5.2.0"
    }
}


pm2 logs

/home/ubuntu/.pm2/pm2.log :
PM2        | [2018-08-16T10:05:55.046Z] PM2 log: ===============================================================================
                                                                ...
PM2        | [2018-08-16T10:07:32.825Z] PM2 log: App [app1] with id [0] and pid [11135], exited with code [1] via signal [SIGINT]
PM2        | [2018-08-16T10:07:32.827Z] PM2 log: Starting execution sequence in -fork mode- for app name:app1 id:0
PM2        | [2018-08-16T10:07:32.828Z] PM2 log: App name:app1 id:0 online
PM2        | [2018-08-16T10:07:33.105Z] PM2 log: App [app1] with id [0] and pid [11145], exited with code [1] via signal [SIGINT]
PM2        | [2018-08-16T10:07:33.106Z] PM2 log: Starting execution sequence in -fork mode- for app name:app1 id:0
PM2        | [2018-08-16T10:07:33.108Z] PM2 log: App name:app1 id:0 online
PM2        | [2018-08-16T10:07:33.383Z] PM2 log: App [app1] with id [0] and pid [11155], exited with code [1] via signal [SIGINT]
PM2        | [2018-08-16T10:07:33.383Z] PM2 log: Script /usr/local/bin/npm had too many unstable restarts (16). Stopped. "errored"

/home/ubuntu/.pm2/logs/app1-error.log :
/home/ubuntu/.pm2/logs/app1-out.log :
                            ...
0|app1   | Specify configs in the ini-formatted file:
0|app1   |     /home/ubuntu/.npmrc
0|app1   | or on the command line via: npm <command> --key value
0|app1   | Config info can be viewed via: npm help config
0|app1   |
0|app1   | [email protected] /usr/local/lib/node_modules/npm
0|app1   |
0|app1   | Usage: npm <command>
0|app1   |
0|app1   | where <command> is one of:
0|app1   |     access, adduser, bin, bugs, c, cache, completion, config,
0|app1   |     ddp, dedupe, deprecate, dist-tag, docs, doctor, edit,
0|app1   |     explore, get, help, help-search, i, init, install,
0|app1   |     install-test, it, link, list, ln, login, logout, ls,
0|app1   |     outdated, owner, pack, ping, prefix, profile, prune,
0|app1   |     publish, rb, rebuild, repo, restart, root, run, run-script,
0|app1   |     s, se, search, set, shrinkwrap, star, stars, start, stop, t,
0|app1   |     team, test, token, tst, un, uninstall, unpublish, unstar,
0|app1   |     up, update, v, version, view, whoami
0|app1   |
0|app1   | npm <command> -h     quick help on <command>
0|app1   | npm -l           display full usage info
0|app1   | npm help <term>  search for help on <term>
0|app1   | npm help npm     involved overview
0|app1   |
                          ...

What does all of this mean, doesn't pm2 recognize the npm command? Is there a parameter I'm missing here? ...

extra info:
server: Ubuntu 16.04
npm version: 5.6.0
nuxt version: 1.4.2
pm2 version: 3.0.3
node version: 8.11.1

Skippy answered 16/8, 2018 at 10:38 Comment(0)
D
12

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.

Disport answered 16/8, 2018 at 14:10 Comment(5)
This works like a charm, thanks! The guide is indeed a must read for people who have the same problem.Skippy
upvoted, how do you specify the mode here though I keep getting an error saying No SSR build! Please start with nuxt start --spa or build using nuxt build --universalCirro
Add "build": "nuxt build --universal" to your package.jsonPrinting
is newer version of nuxt no longer use nuxt-start ? I can't seem to find it in node_modules/nuxt/bin. There's only nuxt.js on that folder. I'm using 2.4.3Ketonuria
@Ketonuria don't know if they removed it can't find it in their release notes, but you can get the nuxt-start package here npmjs.com/package/nuxt-startSkippy
S
14

The code below:

pm2 start npm --name "anyName" -- run dev
Sliwa answered 9/4, 2019 at 13:36 Comment(2)
No way to run it in production via a similar command?Favin
It's okay, I found the official way: nuxtjs.org/faq/deployment-pm2Favin
D
12

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.

Disport answered 16/8, 2018 at 14:10 Comment(5)
This works like a charm, thanks! The guide is indeed a must read for people who have the same problem.Skippy
upvoted, how do you specify the mode here though I keep getting an error saying No SSR build! Please start with nuxt start --spa or build using nuxt build --universalCirro
Add "build": "nuxt build --universal" to your package.jsonPrinting
is newer version of nuxt no longer use nuxt-start ? I can't seem to find it in node_modules/nuxt/bin. There's only nuxt.js on that folder. I'm using 2.4.3Ketonuria
@Ketonuria don't know if they removed it can't find it in their release notes, but you can get the nuxt-start package here npmjs.com/package/nuxt-startSkippy
F
5

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

Favin answered 16/7, 2020 at 18:57 Comment(0)
C
1

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'
    }
  }
}
Cirro answered 2/11, 2019 at 16:48 Comment(0)
M
1

For me this worked for single app https://nuxtjs.org/faq/deployment-pm2/

module.exports = {
  apps: [
    {
      name: 'NuxtAppName',
      exec_mode: 'cluster',
      instances: 'max', // Or a number of instances
      script: './node_modules/nuxt/bin/nuxt.js',
      args: 'start'
    }
  ]
}
Mercurochrome answered 16/10, 2020 at 17:28 Comment(0)
C
0

Try below command if it works

sudo pm2 start npm -- app1:dev
Chromomere answered 16/8, 2018 at 11:35 Comment(1)
This gives the same output in pm2 logs as posted in my question, status=errored as well. logs outputSkippy
A
0
pm2 start ./node_modules/nuxt/bin/nuxt.js --name="<AppName>" -- start
Atwitter answered 31/3, 2021 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.