CodePark/ questions / npm & package.json
START PRACTICINGPRACTICE
Backend question · Node.js › npm & package.json

package.json has "scripts": { "lint": "eslint ." }, and ESLint is installed only as a local devDependency. Why does npm run lint work when typing eslint . in the terminal says command not found?

npm run adds node_modules/.bin to PATH while the script runs

npm run adds node_modules/.bin to PATH while the script runs. PATH is the list of folders the shell searches for commands.

When a package has a
bin entry, npm puts a link to its command in node_modules/.bin. Then npm run puts that folder at the front of PATH for the script's shell. So the shell finds eslint.

"npm scripts are executed by Node, not by a shell" is wrong. The script still runs in a shell:
sh on Unix, and cmd.exe on Windows by default.

Outside npm,
npx eslint . runs the local binary in the same way.