Install and configure Prettier (formatting rules) and ESLint (static code analysis, code quality rules)
Use Prettier for formatting and linters for catching bugs! Install both prettier, eslint and eslint-config-prettier:
npm i -D @fsouza/prettierd eslint_dnpm i --save-dev prettier eslint eslint-config-prettierIs the same as:
npm i -D prettier eslint eslint-config-prettierCreate ESLint config file:
npm init @eslint/configModify Prettier config file accordingly:
- .prettier.json
Modify ESLint config file accordingly:
- .eslintrc.cjs
Select:
- To check syntax, find problems, and enforce code style
- JavaScript modules (import/export)
- None of these
- No
- Node
- Use a popular style guide
- Airbnb: https://github.com/airbnb/javascript
- JavaScript
Then install dependencies:
- eslint-config-airbnb-base@latest
- eslint@^7.32.0 || ^8.2.0
- eslint-plugin-import@^2.25.2
npm i --save-dev prettierCheck which files will be modified by prettier (from project root):
npx prettier src --check
Format all files in src:
prettier src --writenpm i --save-dev eslint
Run eslint on src folder:
npx eslint srcRun eslint on index.js file:
npx eslint index.js
node index.jsnote-node-cli "note 1"nodeInstall Module:
npm install --save-dev jestGet Jest Globals (mocks)
npm install --save-dev @jest/globalsInside (./package.json) add (because we are using modules we have to do that):
{
"scripts": {
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"testWithModules": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
}
}Run test command:
npm test
Run test command:
npm run testWithModules
Inside (./package.json) add:
{
"scripts": {
"testWithoutModules": "jest"
}
}Run test command:
npm run testWithoutModules
Or:
npm testUtility to automatically open the browser from the CLI:
npm i open