How to use Grunt/Gulp with pm2?

后端 未结 2 710
一向
一向 2020-12-30 12:44

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.

相关标签:
2条回答
  • 2020-12-30 13:09

    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.

    0 讨论(0)
  • 2020-12-30 13:10

    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!

    0 讨论(0)
提交回复
热议问题