How Can I run PM2 with Angular-Cli? - Angular2

泪湿孤枕 提交于 2019-12-01 03:36:57

This command would work as expected:

after I run

ng build --prod

then run the following command in the dist/ folder

pm2 start /usr/bin/http-server -- -p 8080 -d false

Update

I have found a better solution: which ng then it will print /usr/bin/ng then type this

pm2 start /usr/bin/ng -- serve --prod

But, if you need ng serve for dev in preprod env, you can create a start.sh at root of your project

#!/bin/bash
ng serve --host xxx.xxx.xxx.xxx --port xxxx

And use pm2 like that :

pm2 start start.sh --name my-pretty-dev-app-run-on-preprod

;)

With PM2 recent version

pm2 ecosystem

than update

ecosystem.config.js as follows

module.exports = {
  apps : [{
    name: 'demoapp',
    script: 'node_modules/@angular/cli/bin/ng',
    args: 'serve --host [yourip] --disable-host-check',
    instances: 1,
    autorestart: true,
    watch: false,
    max_memory_restart: '1G',
    env: {
      NODE_ENV: 'development'
    },
    env_production: {
      NODE_ENV: 'production'
    }
  }],

  deploy : {
  }
};

finally

pm2 start & pm2 save

ng is a node module after all.

apps:
- name: ngserve
  script: 'node_modules/@angular/cli/bin/ng'
  args: 'serve --progress=false --live-reload=false --disable-host-check=true'
  watch: false
  log_date_format: YYYY-MM-DD HH:mm
  merge_logs: true
  out_file: "/dev/null"
  error_file: "/dev/null"

The following, for example, worked for me from the angular project: pm2 start "ng serve --host 0.0.0.0"

This worked for me. The key difference here between this answer and the other answers is that I had to use the cwd option since I was running pm2 from a root directory:

// pm2 start
// https://pm2.io/doc/en/runtime/guide/ecosystem-file
// https://pm2.io/doc/en/runtime/reference/ecosystem-file

module.exports = {
  apps: [{
    name: 'fe',
    script: 'node_modules/@angular/cli/bin/ng',
    args: 'serve -o',
    cwd: 'biblical-hebrew-fe',
    max_restarts: 5,
    min_uptime: 3000,
    exec_mode: 'fork',
    instances: 1, // default
    autorestart: true, // default
    watch: false, // default
    max_memory_restart: '1G', // default
    env: {
      NODE_ENV: 'development'
    },
    env_production: {
      NODE_ENV: 'production'
    }
  }],

  deploy: {
    production: {
      user: 'node',
      host: '212.83.163.1',
      ref: 'origin/master',
      repo: 'git@github.com:repo.git',
      path: '/var/www/production',
      'post-deploy': 'npm install && pm2 reload ecosystem.config.js --env production'
    }
  }
}

If you just want to serve static files, a new command has landed in pm2:

$ pm2 expose [path] [port]

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!