Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy node app that uses grunt to heroku

I'm using grunt and also grunt plugins like grunt-contrib-copy, grunt-contrib-mincss (that listed as npm dependencies for my application).

Also I don't commit npm_modules folder and public folder, where all generated files are. And I can't figure out how to build my app (I have grunt build command) after deploy and setup my server (it's already looking for public folder).

I saw some stuff like grunt-heroku-deploy, but it seems me a bad idea to commit before upload. Maybe there are some gentle decisions... Any thoughts?

like image 351
ValeriiVasin Avatar asked Dec 09 '12 04:12

ValeriiVasin


People also ask

Which is better gulp or grunt?

Gulp vs Grunt: Speed The reason for Gulp's current speed advantage is down to the fact that Gulp uses streams and handles tasks in memory, which means that only one file is written. Furthermore, Gulp can process several tasks at the same time, but Grunt will normally only handle one task at a time.

Do I need to push Node_modules to Heroku?

I am the Node language owner at Heroku and the answer is simple: No. Do not check node_modules in to Heroku apps.


1 Answers

npm has a support for a postinstall step (among many others) that might be just what you're looking for.

The node.js heroku buildpack runs this command when you push to heroku to resolve build dependencies:

$ npm install --production 

https://devcenter.heroku.com/articles/nodejs-support#build-behavior

If you take a look at the npm documentation, you can setup a series of scripts to run either before or after anyone runs npm install for your package. It's configured in the scripts property of package.json. The scripts property allows to run custom scripts (including grunt) when certain things happen in a package's lifecycle.

For example, to echo some text and run the grunt command whenever anyone (including Heroku) runs npm install, add this to your package.json:

{   ...   "scripts": {     "postinstall": "echo postinstall time; ./node_modules/grunt-cli/bin/grunt <your task name>"   },   ... } 

https://npmjs.org/doc/scripts.html

Important caveats:

  • You might have to change the path to the grunt binary in the postinstall script, check the error output if the grunt command doesn't execute.
  • grunt and grunt-cli must be listed as a dependency in your package.json so it gets installed by Heroku. Listing them under devDependencies is not sufficient since Heroku won't install those. Also, note that Heroku won't install it as a global package so to execute it on Heroku you're going to have to use a relative path (as it is configured above).

If this doesn't work (you'll probably need to fiddle with the relative paths a bit), then you might want to consider writing your own custom buildpack for Heroku.

Update

As of 0.4, the grunt package no longer contains the grunt binary, which is now part of the grunt-cli package. The answer has been updated to reflect this.

like image 186
smithclay Avatar answered Sep 22 '22 10:09

smithclay