Search code examples
wordpressprettier

Configuring Prettier format tool for WordPress in .prettierrc


I am trying to use Prettier to format my WordPress project JavaScript / CSS files.

I have installed prettier using npm and I can execute the formatter correctly. I have also installed @wordpress/prettier-config npm module to get WordPress style configuration for Prettier.

Package home page at https://developer.wordpress.org/block-editor/packages/packages-prettier-config/ tells that I need to add

extends @wordpress/prettier-config

to .prettierrc file to enable use of WordPress rules.

This line gives error:

[error] Invalid configuration file `test.js`: Cannot find module 'extends @wordpress/prettier-config'

If I use JSON in .prettierrc

{
    "extends": "@wordpress/prettier-config"
}

I get:

[warn] Ignored unknown option { extends: "@wordpress/prettier-config" }.

and Prettier simply uses default style.

My question is, how do I configure Prettier to use Wordpress style configuration?


Solution

  • If you are using .prettierrc file written in JSON or .prettierrc.json just add:

    "@wordpress/prettier-config"
    

    without brackets.