eslint is not recognized as an internal or external command

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
6 min

banner

# Table of Contents

  1. eslint is not recognized as an internal or external command
  2. 'eslint': command not found error

# eslint is not recognized as an internal or external command

To solve the error "eslint is not recognized as an internal or external command, operable program or batch file":

  1. Install and configure ESLint.
  2. Run the npx eslint command instead of running eslint directly.

eslint is not recognized as internal or external command

Depending on your operating system, the error message might be:

  • 'eslint' is not recognized as an internal or external command, operable program or batch file
  • The term 'eslint' is not recognized as the name of a cmdlet, function, script file, or operable program.
  • 'eslint': command not found

Open your shell in your project's root directory (where package.json is) and install the eslint package with the following command.

cmd
# ๐Ÿ‘‡๏ธ Initialize package.json file if you don't have one npm init -y npm init @eslint/config

install eslint

You can run the eslint command in multiple ways. For example, you can run eslint by using the binary file in the node_modules directory.

cmd
# ๐Ÿ‘‡๏ธ for Windows node_modules\.bin\eslint your_file.js # ๐Ÿ‘‡๏ธ for macOS or Linux ./node_modules/.bin/eslint your_file.js

Make sure to replace the your_file.js placeholder with the name of your actual file.

Alternatively, you can run eslint commands using npx.

cmd
# ๐Ÿ‘‡๏ธ lint the src directory npx eslint src # ๐Ÿ‘‡๏ธ lint your_file.js npx eslint your_file.js

If you get an error that npx is not installed, install it by running the following command.

cmd
# ๐Ÿ‘‡๏ธ for Windows npm install -g npx # ๐Ÿ‘‡๏ธ for macOS or Linux sudo npm install -g npx

install npx

If you get a permissions error when trying to install npx, run CMD as an administrator and rerun the command.

To open CMD as an administrator:

  1. Click on the Search bar and type CMD.

  2. Right-click on the Command Prompt application and click "Run as administrator".

run cmd as administrator

Alternatively, you can add an eslint script in your package.json file.

package.json
{ "scripts": { "lint": "eslint" } }

Then you can run the script with the npm run lint command.

cmd
npm run lint

If the error persists, try restarting your terminal.

If the error persists, you can install the eslint-cli package.

cmd
# ๐Ÿ‘‡๏ธ for Windows npm install -g eslint-cli # ๐Ÿ‘‡๏ธ for macOS or Linux sudo npm install -g eslint-cli
If you get a permissions error, open CMD as an administrator and rerun the npm install -g eslint-cli command.

Then install the eslint package into your project, locally.

cmd
npm install --save-dev eslint

Now you can use the eslint CLI command.

cmd
# ๐Ÿ‘‡๏ธ lint the src directory eslint src # ๐Ÿ‘‡๏ธ lint your_file.js eslint your_file.js

If the error persists, add npm to your PATH environment variable manually.

# Add npm to your PATH environment variable

To add npm to your PATH environment variable:

  1. Click on the Search bar and type "environment variables".
  2. Click on "Edit the system environment variables".

edit system environment variables

  1. Click on the "Environment Variables" button.

click environment variables

  1. In the "System variables" section, select the "Path" variable and click "Edit".

select path and click edit

  1. Click on "New" and then click "Browse".

click new browse

  1. Your npm directory is most likely located under %USERPROFILE%\AppData\Roaming\npm or in other words, C:\Users\YOUR_USER\AppData\Roaming\npm.
location
%USERPROFILE%\AppData\Roaming\npm # ๐Ÿ‘‡๏ธ same as below (make sure to replace YOUR_USER) C:\Users\YOUR_USER\AppData\Roaming\npm

If you can't find it, run the npm config get prefix command.

cmd
npm config get prefix

npm config get prefix

  1. Add the path to npm and click on "OK" twice to confirm.

  2. Close your Command prompt application and then reopen it.

Note that you must restart your Command prompt shell for the changes to take effect.

You might also have to restart your PC, but that's not always necessary.

  1. Try to issue an eslint command after you've restarted your shell.

To solve the error "eslint is not recognized as an internal or external command, operable program or batch file":

  1. Install and configure ESLint.
  2. Run the npx eslint command instead of running eslint directly.

# 'eslint': command not found error [Solved]

The "'eslint': command not found" error occurs when you forget to install the ESLint module on macOS or Linux.

You can solve the error by using the npm init @eslint/config or npx eslint commands.

eslint command not found

Open your terminal (e.g. bash or zsh) in your project's root directory (where package.json is) and install the eslint package with the following command.

shell
# ๐Ÿ‘‡๏ธ initialize package.json file if you don't have one npm init -y npm init @eslint/config

initialize eslint config

You can also run the eslint command by using the binary file in your node_modules directory.

# Running the ESLint command by accessing the binary in node_modules

Open your terminal in your project's root directory and run the following command.

shell
# ๐Ÿ‘‡๏ธ macOS or Linux ./node_modules/.bin/eslint your_file.js # ๐Ÿ‘‡๏ธ Windows node_modules\.bin\eslint your_file.js

Make sure to replace the your_file.js placeholder with the name of your actual file.

Alternatively, you can run eslint commands using npx.

shell
# ๐Ÿ‘‡๏ธ lint the src directory npx eslint src # ๐Ÿ‘‡๏ธ lint your_file.js npx eslint your_file.js

If you get an error that npx is not installed, install it by running the following command.

shell
npm install -g npx

install npx

If you get a permissions error, rerun the command prefixed with sudo.

shell
sudo npm install -g npx

# Adding an eslint script to your package.json file

Alternatively, you can add an eslint script in your package.json file.

package.json
{ "scripts": { "lint": "eslint" } }

Then you can run the script with the npm run lint command.

shell
npm run lint

Make sure to open your terminal (bash or zsh) in the same directory as your package.json file when running the command.

# Install the eslint-cli package globally

If the error persists, install the eslint-cli package globally.

cmd
npm install -g eslint-cli

If you get a permissions error, rerun the command prefixed with sudo.

shell
sudo npm install -g eslint-cli

Then install the eslint package locally.

cmd
npm install --save-dev eslint

Now you should be able to use the eslint CLI command.

cmd
# ๐Ÿ‘‡๏ธ lint the src directory eslint src # ๐Ÿ‘‡๏ธ lint your_file.js eslint your_file.js

If the error persists, add npm to your PATH environment variable manually.

# Adding npm to your PATH environment variable on macOS and Linux

If the error persists, run the following command.

shell
npm config get prefix

The command will show you the path where npm puts your globally installed packages.

The global packages will be in the bin directory at the specified path.

Look at the PATH environment variable on your operating system and add the path that the npm config get prefix command outputs if it's not already there.

If you add the output from the command to your PATH environment variable, you have to restart any open command prompts before it takes effect.

If that didn't work, try to add the path to the bin folder (from npm config get prefix) to your PATH environment variable and restart your terminal.

For example, on macOS, you can update your path with the following command:

shell
# make sure `path` matches with `npm config get prefix` export PATH=/usr/local/share/npm/bin:$PATH

If you are on Linux, you can add the output from the npm config get prefix command to your .bashrc file.

~/.bashrc
# ๐Ÿ‘‡๏ธ make sure to update the path with the output # from the command export PATH="/usr/local/share/npm/bin:$PATH"
If you add the output from the command to your PATH environment variable, you have to restart any open command prompts before it takes effect.

If that doesn't help try to reinstall Node.js on your machine and then install eslint globally by running npm install -g eslint-cli.

During the installation, you might get a prompt for whether you want to automatically update the PATH environment variable on your system, make sure to tick the option.
shell
npm install -g eslint-cli

If the global installation of the eslint-cli fails, rerun the command prefixed with sudo.

shell
sudo npm install -g eslint-cli

Alternatively, you can see how you can fix the permissions error on this page in the official npm docs.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev