How to use ESLint in Node.js Applications?
👨💻Developer ❤️ Nodejs, ReactNative, Expo | 📖 Blogger & Author
ESLint is designed to have all rules completely pluggable. That’s one of the primary reasons it came into existence. It allows developers to create their own linting rules. Every rule provided in the ESLint official guide
is standalone rule and a developer at any point can decide whether to use a specific rule or not.
Note: You must have Node.js installed in order to access ESLint via it’s package manager npm.
For local installation to a project directory:
npm install eslint --save-dev
For a global installation in your working system:
ESLint will now be available via
command in your terminal.
Easiest way to configure it to setup a
JSON file where are all the linting rules can be described.
If you installed eslint globally, you can also generate config file using
Other case, if you have installed it locally to a project, you will need to type in your terminal:
In both cases, you will be prompted with set of basic questions to generate
An example of file generated after above prompt:
For detailed information on Configuration, read here
Rules in ESLint are added individually. No rules are enforced by default. You have to specify rules explicitly, then only it will be enabled for the linting process.
After deciding which rules to include, you have to set there error levels. Each error level can be defined as following:
- Turn the rule off
- Turn the rule on as a warning
- Turn the rule on as an error
The difference between an error and a warning is the exit code that eslint will have when it finishes. If any errors are found, eslint will exit with a 1 exit code, otherwise it will exit with a 0. If you are linting within a build step this allows you to control which rules should "break your build" and which ones should be considered as warnings.
The code you are writing might be suitable for a particular environment such as, you might be writing a REST API in Node.js application using Express Framework (Sinatra) and the frontend of that application is going to be built in AngularJS. Two different projects, two different environments and both can have separate eslint configurations in one file even though the client and the server are under one project directory that is been considered as root of your project.
How is it done? By setting the environment id to true in the "
" section of
ESLint comes with a command line interface (CLI) to lint your files or directory.
The output generated will be grouped by file, and will specify the
number, warning/error, reason for the error, and the name of the rule for each failure.
Use ESLint with your preferred Coding Style
ESLint personally does not promote any coding style. You can setup .eslintrc file to enforce coding style using style rules
that you like.
To add a plugin to
Another plugin, I find very useful is one variable per var
just to maintain code consistency over a large/open-source project.
Thank you for reading. If you find this post useful, please hit the
💚 button so this story can reach out to more readers. If you’d like to talk about it, ping me on Twitter or check my other articles on Node.js.
Want to receive more articles like this one? Subscribe me here. Sometimes, I send “never seen before” content to my subscribers
Subscribe to get your daily round-up of top tech stories!