API Reference

Grunt

Task runner and build tool whose configuration-driven API registers tasks and loads plugins from the gruntfile.

Official Documentation ↗
CompatibilityThis documentation covers version v1. Check the official docs for the latest API surface.

Installation

npmnpm install --save-dev grunt grunt-cli
pnpmpnpm add -D grunt grunt-cli

Methods

Overview

Grunt is a configuration-driven task runner that registers tasks and loads plugins from a gruntfile to automate builds. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • grunt.initConfig — Registers the configuration object that later tasks and plugins read from.
  • grunt.loadNpmTasks — Loads a Grunt plugin so its tasks become available in the gruntfile.
  • grunt.registerTask — Defines a named task alias or custom function composed from other tasks.

Example

module.exports = (grunt) => {
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    clean: ['dist'],
  });
};

Notes

Every task must have both a matching initConfig entry and a loaded plugin, otherwise grunt reports an unknown task. Configuration is read lazily by each task.

Verification

Define and run a build alias that chains two plugin tasks and confirm each speaks to the same configuration keys.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://gruntjs.com/api/grunt · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:50:09.914Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

  • Warnings and Fatal Errors grunt.warn This method is an alias for the grunt.fail.warn method. (https://gruntjs.com/api/grunt)
  • API grunt grunt.config grunt.event grunt.fail grunt.file grunt.log grunt.option grunt.task grunt.template grunt.util Other Inside Tasks Exit Codes Blog Migration Guides Contributing Development Team (https://gruntjs.com/api/grunt)

FAQ

Why does grunt say a task is not found?
The plugin providing the task must be installed and loaded with loadNpmTasks; missing the load call makes the task name unknown.
How do I pass configuration to a task?
Add a key matching the task name under initConfig; tasks read it through grunt.config.get or this.options.
How do I run tasks programmatically?
Load the gruntfile with grunt.tasks([...]) from the grunt module, or require grunt and call grunt.task.run.