0

我试图使用 grunt 并结合 gradle 来配置 jshint,而我正在尝试运行下面的任务是我面临的错误

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

我正面临错误-> 正在加载“jshint.js”任务...错误

以下是我遵循的步骤

包.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
    }
}
4

2 回答 2

0

您是否npm install在项目目录的根目录中运行?您需要在本地安装 JSHint 才能运行 Grunt 任务。:-)

于 2013-10-07T20:19:47.267 回答
0

最后在深入研究我的问题后,我自己找到了解决方案

我犯的错误是 --> 我拼错了 Gruntfile.js 第一个字母是大写的,但我把它写得像下面这样小

files: ['gruntfile.js', 'vM/**/*.js'],
于 2013-10-09T05:15:57.767 回答