Last updated: Apr 4, 2024
Reading timeยท6 min
To solve the error "eslint is not recognized as an internal or external command, operable program or batch file":
npx eslint
command instead of running eslint
directly.Depending on your operating system, the error message might be:
Open your shell in your project's root directory (where package.json
is) and
install the eslint package with the
following command.
# ๐๏ธ Initialize package.json file if you don't have one npm init -y npm init @eslint/config
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.
# ๐๏ธ 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.
# ๐๏ธ 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.
# ๐๏ธ for Windows npm install -g npx # ๐๏ธ for macOS or Linux sudo npm install -g npx
npx
, run CMD as an administrator and rerun the command.To open CMD as an administrator:
Click on the Search bar and type CMD.
Right-click on the Command Prompt application and click "Run as administrator".
Alternatively, you can add an eslint
script in your package.json
file.
{ "scripts": { "lint": "eslint" } }
Then you can run the script with the npm run lint
command.
npm run lint
If the error persists, try restarting your terminal.
If the error persists, you can install the eslint-cli package.
# ๐๏ธ for Windows npm install -g eslint-cli # ๐๏ธ for macOS or Linux sudo npm install -g eslint-cli
npm install -g eslint-cli
command.Then install the eslint
package into your project, locally.
npm install --save-dev eslint
Now you can use the eslint
CLI command.
# ๐๏ธ 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.
npm
to your PATH environment variableTo add npm
to your PATH environment variable:
npm
directory is most likely located under
%USERPROFILE%\AppData\Roaming\npm
or in other words,
C:\Users\YOUR_USER\AppData\Roaming\npm
.%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.
npm config get prefix
Add the path to npm
and click on "OK" twice to confirm.
Close your Command prompt application and then reopen it.
You might also have to restart your PC, but that's not always necessary.
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":
npx eslint
command instead of running eslint
directly.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.
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.
# ๐๏ธ initialize package.json file if you don't have one npm init -y npm init @eslint/config
You can also run the eslint
command by using the binary file in your
node_modules
directory.
Open your terminal in your project's root directory and run the following command.
# ๐๏ธ 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
.
# ๐๏ธ 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.
npm install -g npx
If you get a permissions error, rerun the command prefixed with sudo
.
sudo npm install -g npx
eslint
script to your package.json
fileAlternatively, you can add an eslint
script in your package.json
file.
{ "scripts": { "lint": "eslint" } }
Then you can run the script with the npm run lint
command.
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.
eslint-cli
package globallyIf the error persists, install the eslint-cli package globally.
npm install -g eslint-cli
If you get a permissions error, rerun the command prefixed with sudo
.
sudo npm install -g eslint-cli
Then install the eslint
package locally.
npm install --save-dev eslint
Now you should be able to use the eslint
CLI command.
# ๐๏ธ 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.
npm
to your PATH environment variable on macOS and LinuxIf the error persists, run the following command.
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 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:
# 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.
# ๐๏ธ make sure to update the path with the output # from the command export PATH="/usr/local/share/npm/bin:$PATH"
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
.
npm install -g eslint-cli
If the global installation of the eslint-cli
fails, rerun the command prefixed
with sudo.
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.
You can learn more about the related topics by checking out the following tutorials: