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 Overflow
🌐
Prettier
prettier.io › docs › cli
CLI · Prettier
To run your locally installed version ... exec prettier --help, pnpm exec prettier --help, or bunx prettier --help. To format a file in-place, use --write....
🌐
GitHub
gist.github.com › Mohamed3on › 840b34ecb7e9abf06ace035183b7f1fc
Run prettier on all JS files in a directory · GitHub
Run prettier --write "**/*.js" *Don't forget the quotes. Optional: if you want to format JSON/SCSS files too, replace js with json/scss.
🌐
Prettier
prettier.io › docs › install
Install · Prettier
Prettier will follow rules specified in .gitignore if it exists in the same directory from which it is run. You can also base your .prettierignore on .eslintignore (if you have one). ... If your project isn’t ready to format, say, HTML files yet, add *.html. ... What is that npx thing?
🌐
Fishtank
getfishtank.com › insights › running-prettier-from-the-command-line
Running Prettier from the Command Line | Fishtank
Next is to create a .prettierignore file in your project root directory and specify which file to not format.
🌐
Prettier
prettier.io › docs › configuration
Configuration File · Prettier
Overrides let you have different configuration for certain file extensions, folders and specific files.
🌐
GitHub
github.com › prettier › prettier › discussions › 16169
Force Prettier to run on otherwise ignored file · prettier/prettier · Discussion #16169
doesn't pick it up, nor does explicitly doing the one file: npx prettier --write test.config.js, like OP.
Author   prettier
Find elsewhere
🌐
GoodRequest
goodrequest.com › blog › code-formatting-using-the-prettier-tool
How to format code with Prettier tool step-by-step guide | GoodRequest
July 14, 2023 - If you want to check before manual formatting, enter the command npx prettier --check . Formatting of individual folders / files npx prettier --require-pragma=false --insert-pragma --write [file/dir/glob ...]
Top answer
1 of 1
2

npx will cause a package to be downloaded and execute bin scripts provided by that package. The command npx prettier will cause the latest version of the prettier to be downloaded and the file ./bin/prettier.js will be executed.

npx also allows you specify which specific semantic version you want to download with the @ notation. So npx prettier runs latest, but npx prettier@2 will still run only version 2 even when prettier updates to a new major version.

See npx package docs:

npx [options] <command>[@version] [command-arg]...

and

-p, --package <package> - define the package to be installed. This defaults to the value of <command>. This is only needed for packages with multiple binaries if you want to call one of the other executables, or where the binary name does not match the package name. If this option is provided <command> will be executed as-is, without interpreting @version if it's there. Multiple --package options may be provided, and all the packages specified will be installed.


A somewhat deeper look at your question makes things a little bit weirder though. I'm unsure why you get different results when you run both commands, they should be equivalent (right now at least since 2 is the current major version).

Try printing the version string with both and see if you get a difference, that might show some additional details

npx prettier --version
npx prettier@2 --version

Both give me the same string, but that might be different depending on your cache or config.

🌐
Medium
ceroshjacob.medium.com › formatting-made-easy-a-guide-to-using-prettier-5612be760723
Formatting Made Easy: A Guide to Using Prettier | by Cerosh Jacob | Medium
August 8, 2024 - To format a single file, replace the pattern with the specific file path. Adding the — write tag in the command will directly modify the files selected by the pattern. If there are formatting changes, Prettier will display messages for each ...
🌐
DEV Community
dev.to › bokub › how-to-properly-set-up-prettier-in-less-than-2-minutes-2ld0
How to properly set up Prettier in less than 2 minutes - DEV Community
July 5, 2022 - # With npm npx husky-init npm i -D pretty-quick npx husky set .husky/pre-commit "npx pretty-quick --staged" # With yarn npx husky-init yarn add -D pretty-quick yarn husky set .husky/pre-commit "npx pretty-quick --staged" That’s it ! You will now see this kind of message every time you commit something: The main differences between Prettier and ESLint are the following:
🌐
Krasimirtsonev
krasimirtsonev.com › blog › article › running-prettier-for-my-current-changes-only
Running Prettier for specific Git branch
January 14, 2020 - I had to compare my branch to master, find out what are the changed files and run Prettier only for them. I decided to write a small shell script that will do this job for me. I will use it now and in the future. #!/bin/sh BASEDIR="<path to my project dir>" files=$(git diff --name-status master); while read -r file; do mode=$(echo "$file" | awk '{print $1}') filePath=$(echo "$file" | awk '{print $2}') if [ "$mode" = "M" ] || [ "$mode" = "A" ] || [ "$mode" = "AM" ] then npx prettier --write $filePath fi done <<< "$files"
🌐
GitHub
github.com › prettier › eslint-config-prettier
GitHub - prettier/eslint-config-prettier: Turns off all rules that are unnecessary or might conflict with Prettier. · GitHub
Usually you’ll have about the same rules for all files, so it is good enough to run the command on one file. But if you use multiple configuration files or overrides, you can provide several files to check: npx eslint-config-prettier index.js test/index.js legacy/main.js
Author   prettier
🌐
Prettier
prettier.io › docs › watching-files
Watching For Changes · Prettier
npx onchange "**/*" -- npx prettier --write --ignore-unknown {{changed}}
🌐
Reddit
reddit.com › r/neovim › format your code using prettier without null-ls! just vanilla vim
r/neovim on Reddit: Format your code using prettier without null-ls! Just vanilla vim
July 1, 2023 -

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 command

  • Next 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-filepath option, 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!

🌐
GitHub
github.com › prettier › prettier-vscode › issues › 321
Possible to run prettier across entire project? · Issue #321 · prettier/prettier-vscode
January 8, 2018 - I want to run prettier on every single file in my project, is there a way I can set it up to do so? Reactions are currently unavailable · No one assigned · lockedLocked due to inactivityLocked due to inactivity · No type · No projects · No milestone · None yet · No branches or pull requests ·
Published   Jan 08, 2018
🌐
GitHub
gist.github.com › siakaramalegos › 4a5cdab1f44ffb217a48d5260043f8ae
Adding Prettier to a project · GitHub
Manually run Prettier to re-format all the files in the project: $ npx prettier --write . Set up your code editor to auto-format on save for ease of use. See instructions for various editors. Set up commit hooks with pretty-quick and husky. First, install them as dev dependencies: $ npm i --save-dev pretty-quick husky ·