0

I was trying to configure jshint using grunt and combining with gradle, while I am trying to run my task below is the error that I am facing

Loading "jshint.js" tasks...ERROR
>> Error: Cannot find module 'jshint'
>>     at Function.Module._resolveFilename (module.js:338:15)
>>     at Function.Module._load (module.js:280:25)
>>     at Module.require (module.js:364:17)
>>     at require (module.js:380:17)


No tasks specified, running default tasks.
Running tasks: default

I am facing the error --> Loading "jshint.js" tasks...ERROR

Below are the steps that I followed

package.json

{
  "name": "sample",
  "version": "0.0.0",
  "description": "sample project",
  "main": "scripts/main.js",
  "scripts": {
    "test": "scripts/spec"
  },
  "repository": "",
  "author": "",
  "license": "BSD",
  "devDependencies": {
    "grunt": "~0.4.1",
    "grunt-contrib-jshint": "~0.6.0"
  }
}

Gruntfile.js

module.exports = function(grunt) {
  grunt.initConfig({
      jshint: {
          files: ['gruntfile.js', 'vM/**/*.js'],
          options: {
            globals: {
              jQuery: true,
              console: true,
              module: true
            }
          }
        }
  });

  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.registerTask('test', 'jshint');
};

grunt.gradle

import org.apache.tools.ant.taskdefs.condition.Os
import org.gradle.api.tasks.Exec

def windows_source_dir = "C:\node_modules"

task jshint(type: GruntTask) {
    dependsOn = ['gruntLink']
    gruntArgs = "jshint"
    workingDir 'src/main/webapp/'
}

task gruntLink(type: Exec) {
   description = "Installs all Node.js dependencies defined in package.json"
   if (Os.isFamily(Os.FAMILY_WINDOWS)) {
    commandLine 'cmd', 'mklink /D windows_source_dir src/main/webapp/node_modules'
   } else {
     commandLine = ["ln", "-nfs", "/usr/local/lib/node_modules/grunt_modules", "src/main/webapp/node_modules"]
   }

}

class GruntTask extends Exec {
    private String gruntExecutable = Os.isFamily(Os.FAMILY_WINDOWS) ? "grunt.cmd" : "grunt"

    String gruntArgs = ""

    public GruntTask() {
        super()
        this.setExecutable(gruntExecutable)
    }

    public void setGruntArgs(String gruntArgs) {
        this.args = "$gruntArgs".trim().split(" ") as List
    }
}
Gary
  • 13
  • 1
  • 4

2 Answers2

0

Have you run npm install in the root of your project directory? You need a local installation of JSHint to run the Grunt task. :-)

Ben
  • 10,106
  • 3
  • 40
  • 58
0

Finally after digging into my problem I found solution myself

The mistake I did is --> I spelled Gruntfile.js wrongly the first letter is capital but I gave it as small like below

files: ['gruntfile.js', 'vM/**/*.js'],
Gary
  • 13
  • 1
  • 4