未找到 Grunt 错误任务 "default"

Grunt error task "default" not found

尝试 运行 grunt 时,我收到一条错误消息:

Warning: Task "default" not found. Use --force to continue.
Aborted due to warnings.

我已经找到了好几篇关于这个主题的帖子,每篇帖子的问题都是缺少逗号。但就我而言,我不知道出了什么问题,我想我没有漏掉任何逗号(顺便说一句,此内容 copy/pasted 来自互联网)。

module.exports = (grunt) => {
    grunt.initConfig({
        execute: {
            target: {
                src: ['server.js']
            }
        },
        watch: {
            scripts: {
                files: ['server.js'],
                tasks: ['execute'],
            },
        }
    });

    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-execute');
};

可能是什么问题?

您没有注册默认任务。在最后一个 loadNpmTask

之后添加这个

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

第二个参数是你想从配置中执行什么,你可以放更多的任务。

或者您可以 运行 运行 现有任务,方法是在 cli 中提供名称作为参数。

grunt execute

通过您的配置,您可以使用 executewatch。有关详细信息,请参阅 https://gruntjs.com/api/grunt.task

如果您在终端中 运行 g运行t 它将搜索 "default" 任务,因此您必须注册一个要用 G运行t 执行的任务,用 grunt.registerTask 方法定义它,第一个参数是你的任务名称,第二个参数,它是一个子任务数组,它将 运行.

在你的例子中,代码可能是这样的:

...
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-execute');
grunt.registerTask("default", ["execute", "watch"]);
...

这样,"default" 任务将 运行 分别执行 "execute" 和 "watch" 命令。

但是here您可以找到使用 G运行t 创建任务的文档。

希望对您有所帮助。