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
Answer from Luke Miller on Stack OverflowFormat your code using prettier without null-ls! Just vanilla vim
Why does prettier not show up when the 'which prettier' command is typed?
How do I use the CLI to fix all files?
autocommand to run neoformat prettier on js, ts, jsx and tsx files on save, in lua syntax?
Videos
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've been trying to use as neovim as vanilla possible to expand my horizon and one of the coolest and unknown features out here must be the filter (:!) command. Basically, you can (optionally) redirect part of your buffer as stdin to a shell command which writes it back into your buffer!
As an example, this one way how you could format your code using prettier:
:%!npx prettier --stdin-filepath %
Explanation:
The
%before the!is the range, ie, which part of your buffer do you want to redirect to the command?%means everything, but you could also say for example:.!npx prettier ...where the.would be just the current line, or make a visual selection and write:'<,'>!npx prettier ....The
!is called filter by the vim docs, it calls a shell commandNext comes the command itself, the first part is pretty self explanatory. I don't have prettier installed globally so I use
npx. You have to pass the--stdin-filepathoption, otherwise it won't read your buffer, but the file content (which can differ from your buffer if, for example, you haven't saved your buffer yet). The second%means, in the context of the shell command, "the current file name". (And another cool trick: you can expand it using <c-a>)
That's it, you can use this with any shell command! The beauty of the unix philosophy...
Of course you could also map this to some keybinding.
If you know other cool and hidden tricks of vim, please share them in the comments!
Here is an image of my terminal: https://snipboard.io/iSjD2L.jpg
For reference, I am using a Macbook 2017 Bash terminal.