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_modulesis.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.
First option is the correct way. You don’t commit the node_modules folder, but instead just instruct users to do
npm installwhich fetches all the required dependencies.