How Can I run PM2 with Angular-Cli? - Angular2
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
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, 2020Comments
-
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 afterng 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.
I test with http-server because I think I maybe can run pm2 through this command
pm2 start my_app_process.json
wheremy_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 over 7 yearsI tried your method to run my application and have an error in pm2 log file.
SyntaxError: missing ) after argument list
-
Kevin Suttle over 6 yearsYou can also do
pm2 start $(which ng)
-
renab over 6 yearsThis answer doesn't really have anything to do with how to serve an angular2 app with pm2
-
Prasad Shinde about 5 years@Julien Moulin, is there any other way then .sh file? using exisitng .json file can we do this?
-
FarukT about 5 yearsIt 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 about 5 yearsgreat answer, deserves more upvotes - works on latest pm2 version - although see the tweak I had to make to this in my answer
-
ArrchanaMohan over 4 yearsYour answer saved lot of time.
-
Reza about 4 yearsI tried that, it shows the instance status started, but it's not serving
-
user3486308 almost 4 yearsI 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 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.