Community Post

How to start with ESLint, NPM ESLint

deepakshrma
👁️ 0 views
💬 comments

NodeJs is a dynamic typing language. Here dynamic typed language means, There will be no compile time exceptions. User won’t get any exception like variable not defined before user executes the script. This creates lots of issue after app development. By mistake we design our code in such way it became the house of anti-design patterns. To avoid these anti-design patterns, you can read multiple books but it is time consuming. Same you have to train others developers too. In JS environment there is lots of tool available to validate JavaScript like JSLint, JSHint and ESLint. I have used all of them. In these, I like ESLint very much. It almost works as the others tool. However, there are multiple things which make ESLint different from others. Things I like about ESLint:

  1. Continuous supports and updates.
  2. Easy to install
  3. Lots of plugin around it
  4. Easy to start with

    Working with ESLint CLI tool:

    There are multiple ways you can use ESLint. Either as plugin in Gulp/Grunt or you can use ESLint as CLI tool. Functionality will be same in both cases. However, working with cli tool is easy. This is because you don’t have to setup Gulp/Grunt.

    Install ESLint:

    You need to install npm (a node js based package manager) before installing ESLint.

    $ npm i -g eslint

    Initilize configuration:

    Once finished with installation, you are all set for running tool. If you are not familiar with the rules of ESLint, ESLint provides you to set up basic configuration. For that you have to just pass --init as command line argument. This will prompt a CLI based option list which will ask for your preferences. You can use arrow and spacebar to select your preferences.For more information read: http://eslint.org/docs/user-guide/configuring

    $ cd project_name
    $ eslint --init

    Update ESLint configuration file:

    Once you finished initialization, you can remove some of the basic checks which may not requires. Go to ESLint config file, in my case it is .eslintrc.json. Because I had selected JSON as configuration file and remove some of the checks (for lines) which are not required. Like- "valid-jsdoc": "error". Since i dont created any JSDoc. Checkout my gist for basic configuration (https://gist.github.com/deepakshrma/43955f73c09e245cee278723072017cf). For more information read: http://eslint.org/docs/rules/

    $ gedit .eslintrc.json

    How to run ESLint:

    Once you done with update. Running ESLint is very easy. You just have to link to ESLint binary and pass the file name on which you want to run checks. Here ESLint follows file patterns. For more information read: http://eslint.org/docs/user-guide/command-line-interface

    For Single JS File
    $ eslint file-name.js
    For multiple JS files
    $ eslint file-name1.js file-name2.js
    For multiple JS files inside a directory
    $ eslint dirname/**
    or
    $ eslint dirname/**.js

Tada!! Now you know how to use ESLint. For more information read ESLint. The contributors have created very nice document to read.