Grunt doesn't launch : ">> ReferenceError: grunt is not defined"

Bertrand picture Bertrand · Nov 27, 2013 · Viewed 16.1k times · Source

I'm new to NodeJS and Grunt and I'm struggling to make this work. Here's what I get :

$> grunt
Loading "Gruntfile.js" tasks...ERROR
>> ReferenceError: grunt is not defined
Warning: Task "default" not found. Use --force to continue.

Aborted due to warnings.

Here's my Gruntfile :

module.exports = function(grunt) {
        grunt.initConfig({
                compass: {
                        dist: {
                                options: {
                                        config: 'config/config.rb'
                                }
                        }
                }
        });
};

grunt.loadNpmTasks('grunt-contrib-compass');

grunt.registerTask('default', 'compass');

And here's my package.json :

{
  "name": "tests",
  "version": "0.0.0",
  "description": "Grunt Tests",
  "main": "index.js",
  "devDependencies": {
    "grunt": "~0.4.2",
    "grunt-contrib-compass": "~0.6.0",
    "grunt-contrib-watch": "~0.5.3",
    "grunt-cli": "~0.1.11"
  },
  "scripts": {
    "test": "grunt compass"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/Bertrand31/grunttests.git"
  },
  "keywords": [
    "Grunt",
    "NodeJS",
    "NPM",
    "SASS",
    "Compass"
  ],
  "author": "Bertrand Junqua",
  "license": "GPL",
  "bugs": {
    "url": "https://github.com/Bertrand31/grunttests/issues"
  },
  "homepage": "https://github.com/Bertrand31/grunttests"
}

Oh and I'm running this on a Debian Wheezy.

If you have any idea, let me know. Thanks a lot guys !

Answer

matth picture matth · Nov 27, 2013

You're calling grunt.loadNpmTasks and grunt.registerTask from a scope where grunt is not defined. You'll need to call them within the module.exports function:

module.exports = function(grunt) {
    grunt.initConfig({
            compass: {
                    dist: {
                            options: {
                                    config: 'config/config.rb'
                            }
                    }
            }
    });

    // Call these here instead, where the variable grunt is defined.
    grunt.loadNpmTasks('grunt-contrib-compass');

    grunt.registerTask('default', 'compass');
};