'Next.js with prettier formatting

I'm trying to format my next.js project with prettier. In my package.json I've defined this:

"scripts": {
"format": "prettier --write \"**/*.{js, jsx}\"",
},

So my project has a lot of folders:

  • components
  • layouts
  • pages
  • .next

I obviously don't want to run prettier in my .next folder. How can I ignore some folders?



Solution 1:[1]

Try this:

"scripts": {
"format": "prettier --write \"{,!(.next)/**/}*.{js, jsx}\"",
},

Solution 2:[2]

You can add .prettierignore in the project root directory.

node_modules
.next
out

It uses .gitignore syntax.

Solution 3:[3]

There is a good .prettierignore setup for a base Next.js install on the official github. I use this one a lot as a base to start from.

https://github.com/vercel/next.js/blob/canary/.prettierignore

node_modules
**/.next/**
**/_next/**
**/dist/**
**/__tmp__/**
lerna.json
.github

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jdaz
Solution 2 Nikolai Kiselev
Solution 3