npm jshint 安装和使用 Grunt & nodejs

npm jshint installation and working by Grunt & nodejs

我使用 npm 包在 Windows 上安装了 node.js。
我在 D 盘中有一个项目 D:>projectD
我正在尝试 运行 jshint 以及 SASS、concat 等。一切正常,但在 jshint 上出现错误:

Local npm module "jshint" not found.Is it installed?
Warning: Task 'jshint' not found. Use --force to continue.

安装 jshint 使用了以下命令:

npm install -g jshint
D:>projectD>npm install --save-dev jshint

Gruntfile.js

jshint:{
 all: ['<%= meta.srcPath %>engine/js/myjs1.js']
},

//plugin declaration
grunt.loadNpmTasks('jshint');

// Default task
grunt.registerTask('default', ['concat','sass','jshint']);

Package.json

{
  "name": "Test-Project",
  "version": "0.1.0",
  "devDependencies": {
    "grunt": "~0.4.1",
    "grunt-contrib-concat": "~0.1.3",
    "grunt-sass": "^1.0.0",
    "jshint": "^2.8.0"
  }
}

有人可以帮我解决为什么我不能在 g运行t 中使用 jshint 和其他任务吗?

实际上你的 Gruntfile 是错误的。 你想配置 Grunt。因此,您需要使用 grunt.initConfig

初始化配置

所以在你的情况下

grunt.initConfig({
  jshint: {
    all: ['<%= meta.srcPath %>engine/js/myjs1.js']
  }
});

而且我认为该模块就是您需要的 grunt-contrib-jshint 模块。 所以你的 loadNpmTask 将是 grunt.loadNpmTasks('grunt-contrib-jshint'); 你的 registerTask 只需要包含 jshint 因为你只加载和配置 jshint.

您错过了 module.exports = function(grunt) { }。您需要将配置导出为函数。

总而言之,您的配置应如下所示

module.exports = function(grunt) {

  grunt.initConfig({
    jshint: {
        all: ['<%= meta.srcPath %>engine/js/myjs1.js']
      }
    }
  });

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

  grunt.registerTask('default', ['jshint']);

};