Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automate npm and bower install with grunt

I have a node / angular project that uses npm for backend dependency management and bower for frontend dependency management. I'd like to use a grunt task to do both install commands. I haven't been able to figure out how to do it.

I made an attempt using exec, but it doesn't actually install anything.

module.exports = function(grunt) {      grunt.registerTask('install', 'install the backend and frontend dependencies', function() {         // adapted from http://www.dzone.com/snippets/execute-unix-command-nodejs         var exec = require('child_process').exec,             sys  = require('sys');          function puts(error, stdout, stderr) { console.log(stdout); sys.puts(stdout) }          // assuming this command is run from the root of the repo         exec('bower install', {cwd: './frontend'}, puts);     });  }; 

When I cd into frontend, open up node, and run this code from the console, this works fine. What am I doing wrong in the grunt task?

(I also tried to use the bower and npm APIs, but couldn't make that work either.)

like image 978
Nick Heiner Avatar asked Jan 04 '13 23:01

Nick Heiner


People also ask

Does Bower use npm?

Bower is a command line utility. Install it with npm. Bower requires node, npm and git.

What is Bower and grunt?

Bower belongs to "Front End Package Manager" category of the tech stack, while Grunt can be primarily classified under "JS Build Tools / JS Task Runners".

What is the difference between Bower and npm?

Bower vs NPMNPM is a package system for Javascript. A coder uses it for personal development needs. Bower is a package system for front-end. It is used for web components.


2 Answers

To install client side components during npm install at the same time than server side libs, you can add in your package.json

"dependencies": {     ...     "bower" : "" }, "scripts": {     ...     "postinstall" : "bower install" } 

I prefer to make the difference between install and test/build

like image 149
jsan Avatar answered Sep 28 '22 02:09

jsan


You need to tell grunt that you're using an async method (.exec) by calling the this.async() method, getting a callback, and calling that when exec is done.

This should work:

module.exports = function(grunt) {     grunt.registerTask('install', 'install the backend and frontend dependencies', function() {         var exec = require('child_process').exec;         var cb = this.async();         exec('bower install', {cwd: './frontend'}, function(err, stdout, stderr) {             console.log(stdout);             cb();         });     }); }; 

See Why doesn't my asynchronous task complete?

like image 32
Sindre Sorhus Avatar answered Sep 28 '22 03:09

Sindre Sorhus