Fix a package.json error after installing Prettier
By Flavio Copes
Fix a confusing package.json error after installing the Prettier config for Astro: the real culprit was an empty tailwind.config.js that needed empty braces.
If you get a package.json error right after installing Prettier, and the line number in the error doesn’t exist in your file, the broken file is probably not package.json at all. In my case it was an empty tailwind.config.js.
Here’s the full story.
The error
After installing the prettier config for Astro, I had an error saying I had an error in my package.json file, on a line that didn’t actually exist in such file.
That last part was the clue. The error pointed at a line number beyond the end of the file. I opened package.json, read it top to bottom, and it was perfectly valid JSON. Nothing to fix there.
The real culprit
Turned out I had an empty tailwind.config.js in the project root. I had created the file earlier and never filled it in.
Prettier and its plugins don’t read just one config file when they start up. They resolve a whole chain of them across the project. Somewhere in that chain, the empty tailwind.config.js was loaded, produced nothing where an object was expected, and the failure surfaced with the wrong file name attached to it.
Adding two empty curly brackets fixed the issue:
{
}
That gave the tooling something valid to parse, and the error disappeared.
How to debug this kind of error
When a tool blames a config file that looks fine, check the file really is valid before trusting the error. For package.json you can parse it directly:
node -e "JSON.parse(require('fs').readFileSync('package.json'))"
If this prints nothing, the JSON is fine and the error message is pointing you at the wrong file.
Then look at the other files the tool reads at startup. Config files you created recently are the prime suspects, and empty ones especially. In my case it was tailwind.config.js, but any empty or half-written config file in the project root can trigger the same class of error.
The lesson I took away: error messages tell you something is wrong, but they don’t always tell you where. When the location makes no sense, widen the search to every file the tool touches.
Related posts about tools: