"zsh: command not found: eslint" (and other commands installed via npm)

Viewed 53

I just reset my Macbook and am getting "command not found" after installing packages via npm.

I have a working install of npm and node on my machine:

$ node -v
v18.9.0

$ npm -v
8.19.1

I've used npm install within my project directory, and can see the node_modules folder with the associated commands.

However, when I run something like eslint, I get:

$ eslint 
zsh: command not found: eslint

My path is up-to-date:

$ echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin

I'm assuming that my machine isn't able to find the installed packages, but I don't know how to fix this.

3 Answers

For eslint to be recognized as a command, you need to have it in the PATH and one way to do this is to install the package globally. Which means that running the command will run the package installed globally and not the local one.

If you want to use the package from your local node_modules folder, you need to run it with npx eslint. You could also define an alias in the package.json to run it with npm run eslint.

One last possible solution would be to add the path node_modules/.bin to your PATH.

As others have mentioned in their answers, I was installing packages locally, and I would need to use npx in order to use them.

For this particular project, I am installing eslint to use for syntax checking within Emacs.

For this reason, it makes more sense to install eslint globally to make it easier for Emacs to find the executable, e.g. npm install -g eslint.

I've done this and it's fixed the problem I was having.

Related