How to use Grunt/Gulp with pm2?
Asked Answered
P

2

8

pm2 is a great tool to manage node apps. How does it work with grunt/glup ? I didn't find any useful clues after Googling for 20 minutes.

Participial answered 29/7, 2014 at 1:57 Comment(2)
Please be more specific, do you want to deploy? Do you want to run gulp on restart?Rodeo
@Rodeo deploy. For example, run multiple grunt on server, how to manage these grunt processes like use pm2 to run multiple nodejs processes.Participial
R
6

If I understand your question well, it seems you want to deploy your app.

Since pm2 0.9 deployment can be done with pm2 deploy see README.

In the case of grunt/gulp, I see two options:

  1. You've your node_modules comitted. Using pm2 deploy run your gulp process from the post-deploy section:

    "post-deploy" : "node ./node_modules/gulp/bin/gulp.js ./GulpFile.js && pm2 startOrRestart ecosystem.json --env production"
    
  2. Using a basic script that will launch npm install for you, you could use the package.json to grunt/gulp:

    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "node server.js",
        "postinstall": "./node_modules/bower/bin/bower -q -s -f install && ./node_modules/gulp/bin/gulp.js"
    },
    

My gulp generally needs bower to minify scripts so I left it only for example purpose.

You may combine the two options to let pm2 deploy install your npm scripts and have a postinstall script in the package.json.

Note that I'm using the relative path to the gulp module binary! It's just to avoid an issue if the global module is not installed.

Now, in my opinion to deploy an application in production it's better to simply have a git branch where everything is pre-gulped so that you only clone that branch and you're good to go. It also improves the deploy time, especially if you're running tests with gulp or grunt...

Hope that's clear enough!

Rodeo answered 13/8, 2014 at 14:59 Comment(3)
I happend to have a similar problem. the thing is I want to monitor the grunt running process, could you help me on how to do it ?Furnishing
How about if you're using watch and want the server to reload (read: regulp) on file changes?Curriery
Just use gulp watch instead.Rodeo
P
1

The Reply may be late it must be usefull to others

On the command line do:

$ export NODE_ENV=production

will setup production environmental

$ grunt build

will create necessary min.js and min.css

$ pm2 start server.js

will load the server with pm2 , that its a package thats makes sure the node server will restart if an error and will log.

Photojournalism answered 6/3, 2018 at 5:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.