Alessandro Minoccheri Alessandro Minoccheri - 4 months ago 32
Node.js Question

Grunt install node_modules locally

I have a Gruntfile to install some npm and make other function.

The problem is that: the download of npm works fine but download node_modules globally in


I'd want that the gruntfile download nom locally inside my project dynamically without specify path.

this is a part of my grunt file:

module.exports = function(grunt) {
shell: {
install: {
options: {
stdout: true,
stderr: true
command: [
"npm install grunt-contrib-sass",
"npm install node-sass",
"npm install grunt-contrib-less",
"npm install less",
"npm install grunt-contrib-watch",
"npm install grunt-contrib-clean",
"npm install grunt-contrib-copy",
"npm install grunt-csso",
"npm install grunt-deployments"
install_test: {
options: {
stdout: true,
stderr: true
command: [
"sudo npm install -g phantomjs",
"npm install -g casperjs",
"mkdir app/Test/Frontend",
"sudo chmod -R 777 app/Test/Frontend"

grunt.registerTask("install", [

and after I do this:

sudo npm install grunt
sudo npm install grunt-shell
grunt install

return me error that it doesn't find modules because aren't locally but globally..

How can I solve?



Thats a very crude way of doing the job. Ideally, all of that should go in to package.json in your root folder. put this in you package.json after making necessary changes if any and execute npm install it will install the packages locally and preinstall hook will install global packages beforing executing install.

    "name": "appname",
    "version": "0.0.0",
    "dependencies": {},
    "devDependencies": {
        "grunt": "~0.4.1",
        "grunt-contrib-sass": "*",
        "node-sass": "*",
        "less": "*",
        "grunt-concurrent": "*",
        "grunt-contrib-clean": "*",
        "grunt-contrib-watch": "*",
        "grunt-contrib-less": "*",
        "grunt-contrib-copy": "*",
        "grunt-shell": "*",
        "grunt-csso": "*",
        "grunt-deployments": "*"
    "engines": {
        "node": ">=0.8.0"
    "scripts": {
        "test": "grunt test",
        "preinstall": "sudo npm install -g phantomjs && npm install -g casperjs"

npm gives hooks called preinstall,install,postinstall etc., use them to do -g installs.

follow this LINK for more info

Other tasks you can include in your Gruntfile.js as shell targets