A Prettier plugin that automatically inserts missing commas in multi-line object properties before formatting.
Prettier cannot format code with syntax errors like missing commas in objects. This plugin runs as a preprocessor to insert commas between properties, allowing Prettier to format your code successfully.
- Prettier 3.7.0 or higher
Before — Prettier would throw a parse error:
const config = {
host: 'localhost'
port: 3000
options: {
debug: true
verbose: false
}
}After — commas are inserted, Prettier formats normally:
const config = {
host: 'localhost',
port: 3000,
options: {
debug: true,
verbose: false,
},
};Install prettier-plugin-insert-comma as a dev dependency:
npm install -D prettier prettier-plugin-insert-commaThen add the plugin to your Prettier configuration:
For JSON files, you need to specify the parser explicitly with overrides:
{
"plugins": ["prettier-plugin-insert-comma"],
"overrides": [
{
"files": ["*.json"],
"options": {
"parser": "json",
"quoteProps": "preserve",
"singleQuote": false,
"trailingComma": "none"
}
}
]
}| Language |
|---|
| TypeScript, TSX |
| JavaScript, JSX |
| JSON |