Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run ESLint from NPM script on Windows

In my project I'm trying to implement ESLint as part of a build process that is launched from an npm script.

I've got eslint and all of the plugins I need all installed as npm packages and I've got my .eslintrc file all setup. When I then run the command ...

eslint src

... on a mac, everything works perfectly. I can therefore take that command and stick it into an npm script and it works just fine.

However, on windows I'm experiencing a problem. When I install the eslint npm package I am unable to use eslint from the command line. I see that it has installed in the node_modules directory and that it put the executable in the node_modules/.bin directory, but when I run the command ...

eslint src

... I get an error saying that the eslint command is not found.

I can get it to work if I install eslint and all of my plugins globally, but this isn't ideal because anyone else who clones my code will need to do that also. It's as if on windows, the command line does not have a path mapped to node_modules/.bin.

I tried to solve this problem with this little trick :

PATH=$(npm bin):$PATH eslint src

When I run this command directly from the command line it seems to be able to find the eslint command and everything works perfectly. However, when I place that same command into an npm script, the script runs with no output and no error.

Any direction on how to get this working would be awesome. Thanks.

like image 779
jdavis Avatar asked Feb 15 '16 18:02

jdavis


1 Answers

If you configure your package.json this way

{
  "name": "test",
  "version": "0.1.1",
  "description": "Validate files with ESLint",
  "scripts": {
    "test": "eslint src"
  },
  "devDependencies": {
    "eslint": "^2.0.0",
    "eslint-config-ideal": "^0.1.0"
  }
}

Now just run

npm install

and after that run

npm test

This is should work across OS.

Note: If you run eslint directly then you need to install the eslint globally with npm install eslint -g command.

like image 88
Gyandeep Avatar answered Sep 26 '22 14:09

Gyandeep