How to use Grunt / Gulp with pm2?

pm2 is a great tool for managing node applications. How does this work with grunt / glup? I did not find any helpful tips after Googling for 20 minutes.

+7
javascript gruntjs gulp pm2
source share
2 answers

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

Since pm2 0.9 can be deployed using pm2 deploy see README .

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

  • You have your node_modules . Using pm2 deploy run the gulp process from the post-deploy section:

     "post-deploy" : "node ./node_modules/gulp/bin/gulp.js ./GulpFile.js && pm2 startOrRestart ecosystem.json --env production" 
  • Using a basic script that will run npm install for you, you can use package.json for 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 usually requires a gazebo to minimize scripts, so I left it for an example only.

You can combine the two parameters so that pm2 deploy installs your npm scripts and have a postinstall script in package.json .

Please note that I am using the relative path to the gulp binary module! This is just to avoid the problem if the global module is not installed.

Now, in my opinion, to deploy the application in production, it is better to just have a git branch, where everything is pre-swallowed so that you only clone this branch, and you are good to go. It also improves deployment time, especially if you run tests using gulp or grunt ...

Hope this is clear enough!

+6
source share

The answer may be belated; it must be useful to others.

At the command prompt, do:

 $ export NODE_ENV=production 

establish an ecological environment

 $ grunt build 

will create the necessary min.js and min.css

 $ pm2 start server.js 

will load the server using pm2 , which is its package, which ensures that the node server will restart if an error occurs and will be registered.

+1
source share

All Articles