46
loading...
This website collects cookies to deliver better user experience
npx create-next-app --typescript
# or
yarn create next-app --typescript
--typescript
off if you like to suffer. Your choice, really.cd website
npm install --save-dev --save-exact prettier
# or
yarn add --dev --exact prettier
echo {}> .prettierrc.json
.prettierignore
file. This allows you to list folders and files you don't want to format.touch .prettierignore
.eslintrc
, where it will disable all existing rules that might interfere with Prettier.npm install --save-dev eslint-config-prettier
# or
yarn add --dev eslint-config-prettier
.eslintrc
Next.js created should look like the following:// In file .eslintrc
{
"extends": ["next", "next/core-web-vitals"]
}
// In file .eslintrc
{
"extends": ["next", "next/core-web-vitals", "prettier"]
}
npx mrm@2 lint-staged
package.json
:// In file package.json
// ...
"lint-staged": {
"*.js": "eslint --cache --fix",
"*.{js,css,md}": "prettier --write"
}
// In file package.json
// ...
"lint-staged": {
"*.{js,jsx,ts,tsx,css,md}": "prettier --write"
}
git commit
.# In file .husky/pre-commit
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx lint-staged
# In file .husky/pre-commit
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
yarn lint:write && npx lint-staged
package.json
:// In file package.json
// ...
"scripts": {
// ...
"lint:write": "next lint --cache --fix",
},
--fix
any issues that it finds that can be fixed automatically. Also, the --cache
tells it to only do so on changed files.git commit
, both ESLint and Prettier should check that you don't commit any crap. This should also work when using VSCode's own Git UI. Other Git UIs might have issues though. Sublime Merge for example can't find my node.js installation, most likely because it's installed and managed via nvm. There are almost certainly solutions to this, but since I haven't looked them up yet they fall outside the scope of this tutorial.💡 This post was published 2021/07/14 and last updated 2021/07/14. Since node libraries change and break all the time, there's a non-trivial chance that all of this is obsolete and none of it works when you read this in your flying taxi, gulpin' on insect protein shakes sometime in the distant future or, let's be honest here, next week. If so, tell me in the comments and I'll see if an update would still be relevant.