Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gruntjs - where to install tasks?

Working on a gruntjs 'Hello World' project, and there doesn't seem to be an optimal place to install a grunt task. Say, for instance, that I want to start compiling coffeescript, I would need the 'grunt-coffee' task installed.

Option 1: Install it right into my src tree

This seems to be the way grunt would like you to do it, and it works.

cd $MY_PROJECT_HOME
npm install grunt-coffee
grunt coffee

However, this adds 7.2mg to my project tree. I don't want to put it in my src control, but if I remove it, grunt will not build my project. I could .gitignore it, but then others that download the repository cannot build without doing the same installations. This also gets a bit messy for CI servers.

Option 2: Install it globally

cd $MY_PROJECT_HOME
npm install -g grunt-coffee
grunt coffee

Grunt can't find my plugins if I install them this way:

Local Npm module "grunt-coffee" not found. Is it installed?

It's not clear to me why this wouldn't be supported.

Option 3: Install them somewhere else?

Grunt has an api method called loadTasks, which loads tasks locally. I tried pulling down the npms and moving them myself into a custom directory that I referenced here, with no luck. EG

grunt.loadTasks('$SHARED_TASKS_FOR_ALL_MY_GRUNT_PROJECTS/node_modules/grunt-coffee')

and then:

cd $SHARED_TASKS_FOR_ALL_MY_GRUNT_PROJECTS
npm install grunt-coffee
cd $MY_PROJECT_HOME
grunt coffee

Task "coffee" not found. Use --force to continue.

Option 4: Grunt, in its loadNpmTasks call, pulls down dependencies for me in a .grunt directory somewhere

That'd be nice... :)


EDIT

Sindre below is correct. Option 1 is the way to go, but there is one part missing - the package.json file. So:

  • Add a package.json file and put all of your grunt project's dependencies in there.
  • Ensure that node_modules is .gitignore-ed.
  • In your README, give some instructions to run npm install (note, no arguments) upon a clone or if they add dependencies to the build file.
like image 247
Roy Truelove Avatar asked Sep 18 '12 20:09

Roy Truelove


People also ask

How do I install Grunt locally to my project?

Installing grunt-cli locally js method to get started with a project ( npm install && npm test ) then install grunt-cli locally with npm install grunt-cli --save-dev . Then add a script to your package. json to run the associated grunt command: "scripts": { "test": "grunt test" } .

How do I run a Grunt in terminal?

Run sudo npm install -g grunt-cli (Windows users should omit "sudo ", and may need to run the command-line with elevated privileges). The grunt command-line interface comes with a series of options. Use grunt -h from your terminal to show these options.


1 Answers

First option is the correct way. You don't commit the node_modules folder, but instead just instruct users to do npm install which fetches all the required dependencies.

like image 81
Sindre Sorhus Avatar answered Sep 18 '22 06:09

Sindre Sorhus