Add linting to NodeJs and TypeScript project

Add linting to NodeJs and TypeScript project

We are going to install TypeScript ESLint for finding potential problems with our code.

Most online tutorials talk about using TSLint for TypeScript, but it is deprecated now in favour of ESLint.

Let’s start by installing ESLint.

$ npm i --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin

To configure the linter, we need to create a config called .eslintrc.js in the root directory.

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  plugins: [
    '@typescript-eslint',
  ],
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
  ],
};

Let’s create an ignore file in the project root directory to exclude node modules from listing. The file should be named .eslintignore

# don't ever lint node_modules
node_modules

Once the linter setup is complete, it can be launched with the following command.

$ npx eslint . --ext .js,.jsx,.ts,.tsx

Leave a Reply

Your email address will not be published.