You can do that using lint-staged:

Linting makes more sense when running before committing your code. By doing that you can ensure no errors are going into repository and enforce code style. But running a lint process on a whole project is slow and linting results can be irrelevant. Ultimately you only want to lint files that will be committed.

This project contains a script that will run arbitrary npm and shell tasks with a list of staged files as an argument, filtered by a specified glob pattern.

Install lint-staged and husky, which is required for pre-commit hooks, with this command:

npm install --save-dev lint-staged husky

Change your package.json as follows:

{
  "scripts": {
    "precommit": "lint-staged"
  },
  "lint-staged": {
    "*.js": [
      "prettier --write",
      "git add"
    ]
  }
}
Answer from str 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')"
🌐
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” ...
🌐
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....
🌐
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
🌐
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
🌐
Azz
azz.github.io › prettier › docs › en › usage.html
Usage · Prettier
npm install [--save-dev|--global] prettier · Run Prettier through the CLI with this script. Run it without any arguments to see the options. To format a file in-place, use --write. You may want to consider committing your code before doing that, just in case.
Find elsewhere
🌐
GitBook
basarat.gitbook.io › typescript › intro-2 › prettier
Prettier | TypeScript Deep Dive
"prettier:base": "prettier --parser typescript --single-quote", "prettier:check": "npm run prettier:base -- --list-different \"src/**/*.{ts,tsx}\"", "prettier:write": "npm run prettier:base -- --write \"src/**/*.{ts,tsx}\"" On your build server: npm run prettier:check ·
🌐
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 ·
Author   meeoh
🌐
GitHub
btholt.github.io › complete-intro-to-react-v5 › eslint-prettier
npm, ESLint & Prettier
Don't write them! It puts them in for you. I love Prettier. Need to tool around a bit with it before you trust it? Go here. You can see how it works. Let's go integrate this into our project. It's pretty easy (lol.) Either install Prettier globally npm install --global prettier or replace when I run prettier with (from the root of your project) npx prettier.
🌐
GitHub
github.com › prettier › prettier › issues › 4909
Different config used when running Prettier from npm script · Issue #4909 · prettier/prettier
April 2, 2018 - { "scripts": { "fix": "prettier --write package.json" }, "devDependencies": { "prettier": "^1.14.0" }, "lint-staged": { "*.js": [ "eslint --fix", "git add" ], "*.{json,md}": [ "prettier --write", "git add" ], "*.scss": [ "prettier --write", "stylelint --fix", "git add" ] } }
Author   charlestati
🌐
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
Author   joaocasarin
🌐
npm
npmjs.com › package › prettier-eslint-cli
prettier-eslint-cli - npm
By default prettier-eslint will simply log the formatted version to the terminal. If you want to overwrite the file itself (a common use-case) then add --write.
      » npm install prettier-eslint-cli
    
Published   Oct 14, 2023
Version   8.0.1
Author   Kent C. Dodds
🌐
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 - Now that Prettier is installed and configured, you can actually run it on your existing code to properly format it. ... Prettier will replace all your existing code with properly formatted code. You can add this command to a npm script so you can run it when you need to, but I think it’s ...
🌐
Holt
react-v8.holt.courses › lessons › js-tools › prettier
Prettier – Complete Intro to React v8
First run npm install -D prettier@2.7.1 -D means it's for development only. "scripts": { "format": "prettier --write \"src/**/*.{js,jsx}\"" },
🌐
Medium
thiraphat-ps-dev.medium.com › how-to-use-prettier-to-format-your-code-a-comprehensive-guide-6baad28c383d
How to Use Prettier to Format Your Code: A Comprehensive Guide | by Thiraphat Phutson | Medium
June 5, 2024 - Here’s an example configuration: ...tsx,json,css,scss,md}\"" } Run Prettier To format your code, run the following command: npm run format ·...
🌐
Bitstack
blog.bitsrc.io › add-prettier-to-your-project-d7e91ac03d05
How to Add Prettier to a Project. Format code in your project with… | by Ivo Nederlof | Bits and Pieces
March 4, 2025 - Add files or folders you want to to ignore, for example: ... ... "version": "0.0.0", "scripts": { "start": "node index.js", "format": "npx prettier --write ." }, ... When we run this script, it will reformat all the files in your project.