Gruntfile.js - 找不到任务“default”

24

这是我的Gruntfile.js文件

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON("package.json"),

        uglify: {
          options: {
            mangle: true
          }
          build: {
            src: "js/*.js",
            dest: "js/min/script.js"
          }
        }

    });

    grunt.loadNpmTasks("grunt-contrib-uglify");

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

};

以下是我的package.json文件 - 我已经使用npm install命令安装了我在Gruntfile中要用到的所有插件,其中包括grunt-contrib-uglify。

{
  "name": "bootbuckle",
  "version": "0.1.0",
  "engines": {
    "node": ">= 0.10.0"
  },
  "devDependencies": {
    "grunt": "~0.4.2",
    "grunt-contrib-watch": "~0.5.3",
    "grunt-contrib-sass": "~0.6.0",
    "grunt-csscomb": "~2.0.1",
    "grunt-contrib-htmlmin": "~0.1.3",
    "grunt-contrib-imagemin": "~0.4.1",
    "grunt-contrib-uglify": "~0.2.7"
  }
}

当我在终端中仅运行grunt时 - 出现以下错误。
  build: {
  ^^^^^
Loading "Gruntfile.js" tasks...ERROR
>> SyntaxError: Unexpected identifier
Warning: Task "default" not found. Use --force to continue.
Aborted due to warnings.

非常感谢您可能提供的任何帮助。


编辑 遵循Matti的指导,我插入了一个缺失的逗号,现在出现了一个新错误。

Loading "Gruntfile.js" tasks...ERROR
>> ReferenceError: uglify is not defined
Warning: Task "default" not found. Use --force to continue.
Aborted due to warnings.
2个回答

33

这里少了一个逗号:

    uglify: {
      options: {
        mangle: true
      }, // <-------
      build: {
        src: "js/*.js",
        dest: "js/min/script.js"
      }
    }

编辑:正如japrescott发布的那样,你必须将uglify任务定义为字符串。

grunt.registerTask('default', ["uglify"]);

1
这就是为什么我喜欢用逗号引导。 - Richard Ayotte
谢谢,我已经插入了缺失的逗号,但是现在出现了一个新的错误。我已经编辑了上面的帖子以反映这一点。 - Kevin Lewis
已修复。非常感谢! - Kevin Lewis
6
这就是为什么我喜欢用不同的语言来引领。 - Luke Puplett

12

试着这样做

  grunt.registerTask('default', ["uglify"]);

这个和Matti的回答的结合修复了它。非常感谢! - Kevin Lewis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接