I'm trying to run Prettier from CLI defining the config in the package.json file as it's explained in Offical Docs
Prettier uses cosmiconfig for configuration file support. This means you can configure prettier via (in order of precedence):
A "prettier" key in your package.json file.
Howewer, using the following command and the folling package.json file, I get the shown error:
Command:
prettier --config package.json ./src/basic-sample.js
package.json:
{
"name": "prettier-getting-started",
"version": "1.0.0",
"description": "Prettier - Getting started",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
},
"keywords": [
"prettier"
],
"author": "Rafa Romero",
"license": "ISC",
"devDependencies": {
"prettier": "2.0.4"
},
"prettier":{
"singleQuote": true
}
}
Result:
[error] Invalid configuration file `src/basic-sample.js`: JSON Error in /Users/rafaromero/prettier-sample/package.json:
[error] LinesAndColumns$1 is not a constructor
I would like to note that if a use a .prettierrc.js file with the same command it works
Use the prettier command to run Prettier from the command line. To run your locally installed version of Prettier, prefix the command with npx or yarn (if you use Yarn), i.e. npx prettier --help , or yarn prettier --help . To format a file in-place, use --write . (Note: This overwrites your files!)
From the directory you want to format, run Prettier with --write : prettier --write . This will format the entire directory recursively with Prettier. If you'd rather not install Prettier globally, then you can achieve the same effect with the npx command (the npm package runner):
I ran into this error while using a .prettierrc.json file, and the problem was that the file was encoded in UTF-16LE, not UTF-8. Saving the file as UTF-8 fixed this for me.
The root cause of this issue was following the documentation that recommended echo {}> .prettierrc.json
to create the file. On Windows powershell, this creates a UTF-16LE file, not a UTF-8 file.
More discussion can be found at this github issue
According to https://prettier.io/docs/en/configuration.html configuration in package.json has the highest precedence, so you should be able to call:
prettier ./src/basic-sample.js
or
prettier --write ./src/basic-sample.js
I'm doing this from within an package.json script, but that shouldn't make any difference.
I also got this error when I was creating an instruction video for Prettier in Windows. As Luke suggested, saving the .prettierrc.json as UTF-8 using notepad++ solved the issue. Rather than using the echo {}> .prettierrc.json
in the command as suggested in the Prettier documentation, it would be better to just create the file using an editor or notepad if you are using Windows.
NodeJS Drills - Running prettier and formatting code through the command line
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With