Quickest Way to Add Eslint to JavaScript and TypeScript Projects
- By : Mydatahack
- Category : Front-End, Web Technologies
- Tags: ESLint, JavaScript, TypeScript
The quickest way to add eslint to your project is using eslint init command. ./node_modules/.bin/eslint –init will create eslintrc.js and install dependencies automatically.
(1) Getting started for JavaScript Application
It is super simple. Just install eslint, use eslint tool with a init flag and follow the command line instruction.
npm i --save-dev eslint ./node_modules/.bin/eslint --init
Then, add scripts to package.json.
"lint": "./node_modules/.bin/eslint ./", "lint-fixup": "./node_modules/.bin/eslint ./ --ext .js,.jsx --fix"
(2) Getting started for TypeScript Application
The same as JavaScript. Note that eslint init will install @typescript-eslint/eslint-plugin @typescript-eslint/parser when you choose yes to typescript option. However the tool uses npm. If you are using yarn it’s better to install it before init.
# install dependencies yarn add -D eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser # Choose TypeScript as an option when we go through the command line tool. ./node_modules/.bin/eslint --init
Here is an example of eslintrc.js created by the command-line tool.
module.exports = { "env": { "browser": true, "es2021": true }, "extends": [ "eslint:recommended", "plugin:@typescript-eslint/recommended" ], "parser": "@typescript-eslint/parser", "parserOptions": { "ecmaVersion": 12, "sourceType": "module" }, "plugins": [ "@typescript-eslint" ], "rules": { } };