Despite the good things about using Eslint, there appears a problem. That is, to be able to use it in your Linux Terminal, you need to have the program installed. Otherwise, no matter how much you try, you’d keep getting “eslint: command not found” in your terminal. The fix sounds simple. Just install it. But the process is a bit trickier than you may think. If your distribution’s repository has the eslint package, good news. Otherwise, get ready to shed some sweat.
Install the ESLint package
Using a package manager
The most probable solution to fixing the “eslint: command not found” error message is to simply install the eslint command on your Linux Machine. Now there are several different ways you could achieve this. The easiest of all is to install the eslint package using your Linux distribution’s package manager. Not all distribution’s have the program available directly in their repositories, however.
To install the eslint package in your Ubuntu, Debian, or Arch Linux machine, follow the below commands:
In Arch Linux
$ sudo pacman -Syu $ sudo pacman -S eslint
$ sudo apt-get update $ sudo apt-get install eslint
The first line of command will update the repositories of your system, while the second line will be responsible for installing the eslint package.
This package might also be available in other distributions like Fedora and CentOS that use their own different repositories of applications. However, I’m unsure as I haven’t tried them myself. You may try to see if such a package is available by using these commands in either Fedora or CentOS:
$ sudo yum makecache $ sudo yum install eslint
If you’re using a distribution that is based on Ubuntu, Debian, or Arch Linux, and use the same repositories, then you’ll be able to install the package as well. Otherwise, there are other methods of installing the eslint package, which we’ll be discussing further in the article.
Most people are more familiar with this method of installing eslint in their systems, which is to use npm, the package manager for nodejs. In fact, initially it was only available through npm. To install the package using the npm package manager, first make sure that you have npm installed. Then, use the following command in your terminal:
$ sudo npm install -g eslint
In the above line of command, the -g flag is very crucial, as it will ensure that the package is installed to be made available throughout the entire system. Also, the sudo command is not required if you had intended to install the program locally in a folder. But to install the package globally using the -g flag, you’ll need to use the sudo command, or login as root in order for the command to work as it will be made available to other users of the system too. IIf you don’t use the sudo command, or haven’t logged in as root to execute the command, you’d end up getting an error like this:
However, you may also wish to install the eslint program for a single code project, and may want to include the eslint executable to be only available inside the project directory. In that case, first enter the project directory using the cd command, and then execute the following in your terminal:
$ sudo npm install eslint
Notice that the -g flag is omitted here, which depicts that the package will be installed locally in the current folder. Now to execute the eslint command, first enter the directory of eslint by using:
$ cd /node_modules/eslint/bin
There’s one other thing you could do if you wish to , to be able to execute this file from any directory. That is, to add the path to this folder to the global PATH variable of your system, so that bourne again shell can trace it when the terminal starts. To do this, add the following line of code to the .bashrc file, located in your home directory:
Doing this will ensure that you can run the command from any other directory you might be in. However, you need to make sure that you type the full name of this file, which is eslint.js, instead of just eslint. This is because the program will not be installed using this method, but will only make the command available everywhere.
Also, if you’re using some other shell than bourne again shell, like zsh, then adding it to the .bashrc file will not work. You need to include it in the startup script for zsh instead.
Hopefully, you should now be able to execute the eslint command without any further issues. There’s one more way for you to install the program, though, which will be shown in the next section, just in case you asked!
Install ESLint from source code
This method of installation is only advised in case you failed to install the command using all the other methods mentioned. Also, in case you want more control over the package, or you want to edit parts of it before installation, this would be the way to go, which is to install the package from its source code.
Before you start out, make sure you have the following programs installed in your machine:
The git command will be required to download the source code which is hosted in GitHub. Once you’ve made this sure, you’re ready to proceed towards the installation.
First, you’d wish to navigate to your project directory, as this method will only be installing the program in your local folder.
Inside the desired directory, download the source code using the git command:
$ git clone https://github.com/eslint/eslint.git
Now enter the folder that got downloaded using the cd command:
$ cd eslint
Lastly, run the below command to install eslint along with all the required dependencies for it, locally:
$ npm install
Alternatives to ESLint
There are actually a whole lot of alternatives available to the eslint command in Linux. The two most popular ones are JSLint and JSHint, which are also quite capable. There are a plethora of alternatives, but not all are as competitive, probably. Some of the other commonly referred alternatives are:
- Standard JS
- tslint (linter for Typescript)
- Prettier (it’s actually just a code formatter)
Does it not sound enough, comrades?