Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run prettier from CLI with config in package.json

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

like image 732
Rafa Romero Avatar asked Apr 19 '20 17:04

Rafa Romero


People also ask

How do I run Prettier from command line?

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!)

How do I run Prettier on an entire project?

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):


3 Answers

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

like image 152
Luke Miller Avatar answered Oct 20 '22 03:10

Luke Miller


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.

like image 38
Geoff Avatar answered Oct 20 '22 02:10

Geoff


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

like image 2
Bout Education Avatar answered Oct 20 '22 04:10

Bout Education