运行grunt只是打开一个记事本文件,而不是实际运行任务

Running grunt just opens up a Notepad file instead of actually running the tasks

本文关键字:运行 文件 任务 记事本 grunt 一个      更新时间:2023-09-26

我正在尝试创建一个基本的Grunt文件来执行一些任务。

问题是,当我从项目目录中执行grunt时,会打开一个记事本文件,显示grunt.js的内容,而不是实际运行。

我也试过命名文件Gruntfile.js,但后来我得到一个消息

grunt-cli: The grunt command line interface. (v0.1.13)
Fatal error: Unable to find local grunt.
If you're seeing this message, either a Gruntfile wasn't found or grunt
hasn't been installed locally to your project. For more information about
installing and configuring grunt, please see the Getting Started guide:

和我已经安装了grunt-cli

grunt.jsGruntfile.js看起来都像:

module.exports = function(grunt) {
    grunt.initConfig({
    });
    grunt.registerTask('foo', 'A sample task that logs stuff.', function(arg1, arg2) {
        if (arguments.length === 0) {
            grunt.log.writeln(this.name + ", no args");
        } else {
            grunt.log.writeln(this.name + ", " + arg1 + " " + arg2);
        }
    });
    grunt.registerTask('default', []);
};

问题是我缺少一个package.json文件。我只是从来没有创建过。

我通过运行

解决了这个问题
npm init

生成

即使grunt是全局安装的,您也需要将其添加到您的项目中-在您的项目目录中运行:

npm install grunt --save-dev

不要重命名你的任务文件(保留Gruntfile.js),然后运行grunt应该工作良好