For *.ts files:
npx prettier 'src/**/*.ts' --write
If you want target other file extensions:
npx prettier 'src/**/*.{js,ts,mjs,cjs,json}' --write
Answer from Tiago Bértolo on Stack OverflowHow to run prettier check ?
node.js - How to configure prettier to check all files with a specific extension - Stack Overflow
Format your code using prettier without null-ls! Just vanilla vim
node.js - npx - what does the `@` do in `npx prettier` vs `npx prettier@2`? - Stack Overflow
Videos
For *.ts files:
npx prettier 'src/**/*.ts' --write
If you want target other file extensions:
npx prettier 'src/**/*.{js,ts,mjs,cjs,json}' --write
To exclude files from formatting, create a .prettierignore file at the root of your project.
And to format only the *.ts files you should ignore everything but the *.ts files.
Example
# Ignore everything recursively
*
# But not the .ts files
!*.ts
# Check subdirectories too
!*/
In the code above, the * means to ignore everything including the subfolders, and the next line !*.ts tells the prettier to reverse the previous ignoring of the .ts files. The last line !*/ means to check the subdirectories too, but with the previous rule, it's only looking for the .ts files.
Check the prettier and gitignore docs for more information.
» npm install prettier
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!
Hi there,
I'm using VS Code on Windows 11, and I have installed Prettier. However, I can only use it from the terminal.
If I execute npx Prettier --write dom.html in the terminal, it works fine.
If I go to the Command Palette and choose Format Document, the following error appears in the output:
["ERROR" - 10:38:49 AM] Invalid prettier configuration file detected. ["ERROR" - 10:38:49 AM] ENOENT: no such file or directory, open 'c:\Users\ivanf\Dropbox\dev\FrontEndBootCamp - Exercises\firstjs\ C:\Users\ivanf\AppData\Roaming\npm\prettier' Error: ENOENT: no such file or directory, open 'c:\Users\ivanf\Dropbox\dev\FrontEndBootCamp - Exercises\firstjs\ C:\Users\ivanf\AppData\Roaming\npm\prettier'
To make it more clear,
c:\Users\ivanf\Dropbox\dev\FrontEndBootCamp - Exercises\firstjs\ is the project folder, and \Users\ivanf\AppData\Roaming\npm\prettier is where prettier is installed.
So it seems that in some configuration file the wrong path "PROJECT_FOLDER PRETTIER_HOME" is formed.
Unfortunately, I cannot locate such file, and I cannot understand why it works when called from the terminal.
Any advice or hints? Thank you in advance, Ivan.