Code Linting

With multiple developers on board, your syntax can get pretty messy if not all of you write code in the same way. Linting) is the process of running a program that analyses code for potential errors. This article describes how to setup this for your Node.js project.

Getting Started

ECMAScript/JavaScript

ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs. Install the package first.

$ npm install --save-dev eslint

Then create a new configuration file `./.eslintrc`.

$ ./node_modules/.bin/eslint --init

Open your `./package.json` file and add a new script.

{
  "scripts": {
    "lint": "eslint 'src/**/*.js?(x)'"
  }
}

You can now verify your syntax.

$ npm run lint

To enable linting for ECMAScript/JavaScript in Atom Text Editor, install linter and linter-eslint plugins. For Visual Studio Code Editor you'll need to install the eslint extension.

TypeScript

The configuration is similar to ECMAScript/JavaScript. You will use TSLint instead. Install the package first.

$ npm install --save-dev tslint

Then create a new configuration file `./tslint.json` and add configuration.

$ ./node_modules/.bin/tslint --init

Open your `./package.json` file and add a new script.

{
  "scripts": {
    "lint": "tslint 'src/**/*.ts?(x)'"
  }
}

You can now verify your syntax.

$ npm run lint

To enable linting for Typescript in Atom Text Editor, install linter and linter-tslint plugins. For Visual Studio Code Editor you'll need to install the tslint extension.

results matching ""

    No results matching ""