There's a great package called prettier-eslint that we've used at work before. It will format using prettier and then run eslint --fix on your code. We liked it, give it a try!
How to get ESLint --fix to also run prettier --write
eslint - How can I solve the prettier/prettier problem - Stack Overflow
A better Prettier CLI and config file
node.js - prettier command not working even though prettier is in devDependancies - Stack Overflow
Videos
This error is related to Windows Line endings being different from Unix ones. Try running npx prettier --write . on your project's directory. This command will tell prettier to fix simple errors (such as this one) when found.
To prevent this error from happening again you can either set "end-of-line" to auto on your .prettierrc file or try setting line endings to "Unix"/LF on your editor: instructions for VSCode and for IntelliJ-based editors
in your .prettierrc file, add this: "endOfLine": "auto"
» npm install prettier-eslint-cli
Installing a module that includes a CLI program like prettier, nodemon, etc., will place the program in node_modules/.bin. Your terminal shell does not know to look in this folder when you run a command. Type echo $PATH in your terminal to see a list of the folders that are checked when you run a command. You'll see that running prettier will be looking in places like /bin, /usr/bin, /usr/local/bin, etc., but definitely not /var/folders/q6/npwl_7xj4wg91lg06f8pnnfh0000gn/T/node_modules.
This is why npx is often used; from their docs:
Executes either from a local node_modules/.bin, or from a central cache, installing any packages needed in order for to run.
You could also run it via ./node_modules/.bin/prettier, or you could install it globally, as you ultimately did, which will put it in a place that is part of your $PATH.
After trying a bunch of things, I noticed it only affected this one folder. So what finally worked was simply cloning the repo again into a different folder.
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.