31

I'm trying to install ESlint to use it with Sublime Text 2 for all my local projects. Configuration documentation is very unclear about global installation:

Note: eslint --init is intended for setting up and configuring ESLint on a per-project basis and will perform a local installation of ESLint and its plugins in the directory in which it is run. If you prefer using a global installation of ESLint, any plugins used in your configuration must also be installed globally.

I don't understand what they mean. I used eslint --init and it installed ESlint locally in node_modules, along with all plugins. There's nothing explained about installing plugins globally. How do I do that? Also, how do I use the global ESlint installation if eslint --init installs local one anyway? This is so confusing.

Ojasvi Monga
  • 4,437
  • 2
  • 18
  • 35
Robo Robok
  • 21,132
  • 17
  • 68
  • 126

4 Answers4

30

You can install Node modules within the project (locally) or globally. To switch to globally, you may use the -g flag, like so:

npm install -g eslint

Then see if it's working without Sublime Text (-v flag to see the version of eslint):

eslint -v

To see where it was installed (assuming MacOS/Linux):

which eslint

Then see if it's working in Sublime Text (you may need to restart Sublime first). If it's not working, make sure in the eslint package settings that the path is correct.

Winfried
  • 9,243
  • 4
  • 26
  • 24
  • 1
    I did that, but `eslint --init` requires some additional plugins, like for example React plugin if I intend to use React. How to make `eslint --init` install all plugins globally? – Robo Robok Mar 14 '18 at 15:54
  • @RoboRobok `eslint --init` installs the packages simply within the `package.json` file of your project. Packages like React are part of your project, not an 'application' which you may install globally on your computer. You can, however, differentiate within `package.json` what kind of package it is within your project, by listing them either within the `dependencies` or the `devDependencies` objects. – Winfried Mar 15 '18 at 13:47
  • 1
    I would prefer to have them globally for the purpose of linting. – Robo Robok Mar 15 '18 at 18:55
  • According to the [eslint docs](https://eslint.org/docs/user-guide/getting-started#global-installation-and-usage), `eslint --init` only installs them locally. You'll need to install them globally yourself, sadly. The feature to make `eslint --init` install global packages seems to be open in [this github issue](https://github.com/eslint/eslint/issues/6732). – Winfried Mar 16 '18 at 09:37
5

The assumption is that you have an eslint plugin installed for your editor,if you have then npm install -g eslint,then you can install add-ons for specific environments,like npm install eslint-config-airbnb eslint-plugin-react eslint-plugin-jsx-a11y eslint-plugin-import -g (this is support for pure JS and for React),you can on this way add support for nodejs too,in working folder make .eslintrc file which looks like this

{
    "extends":  ["airbnb" , "eslint:recommended"],
    "env": {
    "node": false,
    "es6": true,
    "browser": true
    },
    "rules": {
        "semi":"error",
        "no-unused-vars": "off",
        "func-names":"off",
        "indent":"off",
        "no-else-return":"off",
        "prefer-arrow-callback":"off",
        "no-undef":"off",
        "no-use-before-define":"off",
        "comma-dangle":"off",
        "eol-last":"off",
        "no-trailing-spaces":"off",
        "linebreak-style":"off",
        "no-console":"off",
        "no-restricted-globals":"off",
        "object-shorthand":"off",
        "no-shadow":"off",
        "no-debugger":"off",
        "prefer-const":"off",
        "no-multiple-empty-lines":"off"
    }
}

if you need node support then in env section of .eslintrc set node to 'true' and install eslint-node plugin globally too with next npm i eslint-plugin-node -g. Then in extends section of .eslintrc add "plugin:node/recommended". In this way, you will have eslint support in every project on your machine which have .eslintrc file.Set rules which you need in .eslintrc rules section . Thats it.

grrigore
  • 1,050
  • 1
  • 21
  • 39
Goran_Ilic_Ilke
  • 812
  • 12
  • 16
4
  1. To install eslint globally: npm install -g eslint

    To install eslint in your project folder: npm install eslint --save-dev

  2. Add in package.json this script : "eslint": "eslint --ignore-path .gitignore ."

  3. Create a file called .eslintrc and insert :

    {
      "env": {
        "browser": true,
        "node": true
      },
      "globals": {
        "chrome": true
      },
      "rules": {
        "no-console": 0,
        "no-empty": [1, { "allowEmptyCatch": true }]
      },
      "extends": "eslint:recommended"
    }
    

    Personally, I save this file in my js folder

  4. Go to node_modules/.bin

  5. Run : eslint --init or npm run eslint nameOfYourFile
Mickael Maison
  • 25,067
  • 7
  • 71
  • 68
Marina ES
  • 250
  • 2
  • 6
0

Unfortunately, ESLint no longer recommends the use of Personal Configuration. Even if you have ESLint and other ESLint configuration files installed in the global scope, it will not read them correctly.

https://eslint.org/docs/latest/user-guide/configuring/configuration-files#personal-configuration-files-deprecated

Personal Configuration Files (deprecated)

⚠️ This feature has been deprecated. This feature will be removed in the 8.0.0 release. If you want to continue to use personal configuration files, please use the --config CLI option. For more information regarding this decision, please see RFC 28 and RFC 32.

~/ refers to the home directory of the current user on your preferred operating system. The personal configuration file being referred to here is ~/.eslintrc.* file, which is currently handled differently than other configuration files.

How does ESLint find personal configuration files?

If eslint could not find any configuration file in the project, eslint loads ~/.eslintrc.* file.

If eslint could find configuration files in the project, eslint ignores ~/.eslintrc.* file even if it's in an ancestor directory of the project directory.

How do personal configuration files behave?

~/.eslintrc.* files behave similarly to regular configuration files, with some exceptions:

~/.eslintrc.* files load shareable configs and custom parsers from ~/node_modules/ – similarly to require() – in the user's home directory. Please note that it doesn't load global-installed packages.

~/.eslintrc.* files load plugins from $CWD/node_modules by default in order to identify plugins uniquely. If you want to use plugins with ~/.eslintrc.* files, plugins must be installed locally per project. Alternatively, you can use the --resolve-plugins-relative-to CLI option to change the location from which ESLint loads plugins.

nnmax
  • 71
  • 4