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 › install
Install · Prettier
In addition to running Prettier from the command line (prettier --write), checking formatting in CI, and running Prettier from your editor, many people like to run Prettier as a pre-commit hook as well. This makes sure all your commits are formatted, without having to wait for your CI build to finish. For example, you can do the following to have Prettier run before each commit: ... npm install --save-dev husky lint-staged npx husky init node --eval "fs.writeFileSync('.husky/pre-commit','npx lint-staged\n')"
🌐
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....
🌐
Fishtank
getfishtank.com › insights › running-prettier-from-the-command-line
Running Prettier from the Command Line | Fishtank
"format": "prettier --loglevel warn --write \"{<file-path>}/**/*.{jsx,js}\"", The example above is from a project on a mac os. Now format all files with Prettier by running - npm run format in your terminal. This command will pick up “format” inside “script” in your project’s package. JSON file. npm run format · Alternatively; NPM · npx prettier --write .
🌐
GitHub
github.com › alshedivat › al-folio › discussions › 2623
How to run prettier check ? · alshedivat/al-folio · Discussion #2623
"Then, you can install it using npm install prettier inside the project directory, or install it globally on your computer using npm install -g prettier. To run prettier on your current directory use npx prettier . --write." Works for me.
Author   alshedivat
🌐
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 - Prettier provides the ability to format the entire project manually with a command npx prettier --write.
🌐
Prettier
prettier.io › docs › watching-files
Watching For Changes · Prettier
npx onchange "**/*" -- npx prettier --write --ignore-unknown {{changed}}
Find elsewhere
🌐
Medium
kazemmdev.medium.com › transform-your-codebase-with-prettier-a-guide-with-husky-integration-e4eb35c53436
Transform Your Codebase with Prettier: A Guide with Husky Integration | by Kazem Mirzaei | Medium
March 13, 2023 - npx prettier --write script.js · This will format all the JavaScript files in your project and write the changes to the files. 2. Use an editor plugin · If you prefer to use an editor, you can install a plugin that integrates with Prettier.
🌐
GitHub
github.com › prettier › prettier › issues › 12865
`prettier --write` doesn't work when in a package.json script · Issue #12865 · prettier/prettier
May 14, 2022 - Prettier 2.6.2 Playground link --parser babel --print-width 100 --tab-width 4 --single-quote --trailing-comma none Input: export {} Output: export {}; Current behavior running yarn prettier --write "**/*.{js,ts}" in the terminal: It does...
Published   May 14, 2022
🌐
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:
🌐
GitHub
github.com › jhipster › generator-jhipster › issues › 8299
Launch prettier with npx, not npm · Issue #8299 · jhipster/generator-jhipster
September 12, 2018 - run npm prettier --write 'src/**/*.{ts,tsx,css,scss}' --> see the error · Change the command npx prettier --write \"src/**/*.{ts,tsx,css,scss}\"" I had to use double quotes instead of single quotes, otherwise when launching npm run prettier:format file scan fails.
Published   Sep 12, 2018
🌐
GitHub
btholt.github.io › complete-intro-to-react-v5 › eslint-prettier
npm, ESLint & Prettier
From there, run prettier script.js. This will output the formatted version of your file. If you want to actually write the file, run prettier --write script.js. Go check script.js and see it has been reformatted a bit. I will say for non-JSX React, prettier makes your code less readable.
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.

🌐
Scarb
docs.swmansion.com › setup-ci › docs › workflows › prettier
Prettier check | npx setup-ci
Learn more about Prettier: prettier.io · npx setup-ci --preset --prettier · Below you can find detailed information about what the script does with your project when generating Prettier check workflow. prettier (dev) The following diagram represents the flow of the Prettier check CI workflow: The .prettierrc configuration file generated by SCI might not be well tailored to your project.
🌐
npm
npmjs.com › package › prettier
prettier - npm
Prettier can be run in your editor on-save, in a pre-commit hook, or in CI environments to ensure your codebase has a consistent style without devs ever having to post a nit-picky comment on a code review ever again!
      » npm install prettier
    
Published   Jan 21, 2026
Version   3.8.1
Author   James Long
🌐
Holt
react-v8.holt.courses › lessons › js-tools › prettier
Prettier – Complete Intro to React v8
From there, run prettier src/App.js. This will output the formatted version of your file. If you want to actually write the file, run prettier --write src/App.js. Go check src/App.js and see it has been reformatted a bit. I will say for non-JSX React, prettier makes your code less readable.
🌐
GitHub
gist.github.com › siakaramalegos › 4a5cdab1f44ffb217a48d5260043f8ae
Adding Prettier to a project · GitHub
$ 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 ...