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

22,186

Solution 1

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

Solution 2

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

;)

Solution 3

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

Share:
22,186
Vicheanak
Author by

Vicheanak

I'm a Javascript full stack developer experiencing in NodeJS, Angular1, Angular2, Ionic1, Ionic2 and JQuery. I'm also very experienced in Linux Server such as setting up development environment and deployment procedure. I'm a big fan of Git and VIM is his favorite editor.

Updated on April 16, 2020

Comments

  • Vicheanak
    Vicheanak about 4 years

    How can I run: ng serve --prod with pm2?

    ng serve from angular-cli, Angular2. I'm running on DigitalOcean.

    I have tried to test with http-server -p 4200 -d false in the dist/ folder after ng build --prod

    When I request from the domain https://www.unibookkh.com/, i got 404 error: (I've already setup nginx to listen to port 4200.

    enter image description here

    I test with http-server because I think I maybe can run pm2 through this command pm2 start my_app_process.json where

    my_app_process.json

    {
        "apps": [
            {
                "name": "angular",
                "cwd": "~/angular2",
                "args": "-p 4200 -d false",
                "script": "/usr/bin/http-server"
            }
        ]
    }
    

    Any better ideas of how to get it working with PM2?

  • Edward
    Edward over 7 years
    I tried your method to run my application and have an error in pm2 log file. SyntaxError: missing ) after argument list
  • Kevin Suttle
    Kevin Suttle over 6 years
    You can also do pm2 start $(which ng)
  • renab
    renab over 6 years
    This answer doesn't really have anything to do with how to serve an angular2 app with pm2
  • Prasad Shinde
    Prasad Shinde about 5 years
    @Julien Moulin, is there any other way then .sh file? using exisitng .json file can we do this?
  • FarukT
    FarukT about 5 years
    It works perfectly but uses lots of memory (I know this is not a problem but a simple application less than 100 lines uses more than 400mb). of course this is a problem of angular app. I think the best way is build app for production and make a simple sh file and run it
  • danday74
    danday74 about 5 years
    great answer, deserves more upvotes - works on latest pm2 version - although see the tweak I had to make to this in my answer
  • ArrchanaMohan
    ArrchanaMohan over 4 years
    Your answer saved lot of time.
  • Reza
    Reza about 4 years
    I tried that, it shows the instance status started, but it's not serving
  • user3486308
    user3486308 almost 4 years
    I have compeletely different content, so should I delet them all and put your code there? Should I modify app name or [your ip]? How?
  • Bheru Lal Lohar
    Bheru Lal Lohar over 3 years
    @user3486308 Yes you need to update the [app name] and [IP], Most important are script and args you can add these in your current config. if any issue please share config file via pastebin.