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_d
npm i --save-dev prettier eslint eslint-config-prettier
Is the same as:
npm i -D prettier eslint eslint-config-prettier
Create ESLint config file:
npm init @eslint/config
Modify 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 prettier
Check which files will be modified by prettier (from project root):
npx prettier src --check
Format all files in src:
prettier src --write
npm i --save-dev eslint
Run eslint on src folder:
npx eslint src
Run eslint on index.js file:
npx eslint index.js
node index.js
note-node-cli "note 1"
node
Install Module:
npm install --save-dev jest
Get Jest Globals (mocks)
npm install --save-dev @jest/globals
Inside (./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 test
Utility to automatically open the browser from the CLI:
npm i open