Grunt Prettier
raw JSON → 2.2.0 verified Sat Apr 25 auth: no javascript
Grunt plugin to run Prettier on your files. Integrates the Prettier code formatter into Grunt workflows. Current stable version is 2.2.0, released with support for Prettier v2 and config overrides. Release cadence is irregular. Differentiates from running Prettier via CLI by providing a Grunt task that supports multiple target configurations, file-to-file formatting, concatenation with formatting, and in-place overwriting. Options can be specified in task config or .prettierrc, merged with defaults.
Common errors
error Warning: Task "prettier" not found. ↓
cause grunt.loadNpmTasks('grunt-prettier') not called or plugin not installed.
fix
Ensure you run npm install grunt-prettier --save-dev and add grunt.loadNpmTasks('grunt-prettier') in Gruntfile.
error Cannot find module 'prettier' ↓
cause Prettier not installed as a peer dependency.
fix
Run: npm install prettier --save-dev
error Fatal error: unable to format files ↓
cause Incorrect file configuration (missing 'src' or 'files' property).
fix
Define either files.src (array of globs) or files mapping (source to dest).
Warnings
breaking Version 2.0 updated prettier to v2, which includes breaking changes (e.g., trailingComma default changed). ↓
fix Review prettier v2 changelog and update your options accordingly.
gotcha Config overrides only work in .prettierrc, not in the Grunt task config. ↓
fix Specify overrides in a .prettierrc file adjacent to your Gruntfile.
gotcha The 'dest' format concatenates files before formatting; 'src' format overwrites in place. ↓
fix Use 'src' for in-place formatting; use 'files' mapping for file-to-file or concat+format.
deprecated Node.js >=10 required; older versions may not work with recent prettier. ↓
fix Upgrade Node to at least 10.0.0.
Install
npm install grunt-prettier yarn add grunt-prettier pnpm add grunt-prettier Imports
- grunt-prettier wrong
grunt.loadNpmTasks('grunt-prettier'); (no wrong pattern, but note: must be called after installing)correctgrunt.loadNpmTasks('grunt-prettier'); - prettier wrong
import prettier from 'prettier'; // ESM not supported in Node<14correctconst prettier = require('prettier'); - grunt
const grunt = require('grunt');
Quickstart
// Install: npm install grunt grunt-prettier prettier --save-dev
// Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
prettier: {
options: {
singleQuote: true,
useTabs: false
},
files: {
// Format all JS files in src/ and write back
src: ['src/**/*.js']
}
}
});
grunt.loadNpmTasks('grunt-prettier');
grunt.registerTask('default', ['prettier']);
};