pavva91 / nodejs-v3

Kitchen sink Node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Node.js v3

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:

NOTE: For The editor use the Daemons

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:

  1. To check syntax, find problems, and enforce code style
  2. JavaScript modules (import/export)
  3. None of these
  4. No
  5. Node
  6. Use a popular style guide
  7. Airbnb: https://github.com/airbnb/javascript
  8. JavaScript

Then install dependencies:

  • eslint-config-airbnb-base@latest
  • eslint@^7.32.0 || ^8.2.0
  • eslint-plugin-import@^2.25.2

Use Prettier

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

Use ESLint

npm i --save-dev eslint

Run eslint on src folder:

npx eslint src

Run eslint on index.js file:

npx eslint index.js

Execute JS program

node index.js

Execute JS CLI

note-node-cli "note 1"

Enter Node REPL

node

Test (Jest)

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

Vanilla Server

Utility to automatically open the browser from the CLI:

npm i open

About

Kitchen sink Node.js


Languages

Language:JavaScript 91.0%Language:Shell 6.5%Language:HTML 2.5%