简体   繁体   English

如何全局安装 ESlint?

[英]How to install ESlint globally?

I'm trying to install ESlint to use it with Sublime Text 2 for all my local projects.我正在尝试安装 ESlint 以将其与 Sublime Text 2 一起用于我所有的本地项目。 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.注意: eslint --init用于在每个项目的基础上设置和配置 ESLint,并将在运行它的目录中执行 ESLint 及其插件的本地安装。 If you prefer using a global installation of ESLint, any plugins used in your configuration must also be installed globally.如果您更喜欢使用 ESLint 的全局安装,则配置中使用的任何插件也必须全局安装。

I don't understand what they mean.我不明白他们的意思。 I used eslint --init and it installed ESlint locally in node_modules , along with all plugins.我使用eslint --init并在 node_modules 本地安装了node_modules以及所有插件。 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?另外,如果eslint --init安装了本地的,我该如何使用全局 ESlint 安装? This is so confusing.这太令人困惑了。

You can install Node modules within the project (locally) or globally. 您可以在项目中(本地)或全局安装节点模块。 To switch to globally, you may use the -g flag , like so: 要切换到全局,您可以使用-g标志 ,如下所示:

npm install -g eslint

Then see if it's working without Sublime Text ( -v flag to see the version of eslint): 然后查看它是否在没有Sublime Text的情况下工作( -v标志可以看到eslint的版本):

eslint -v

To see where it was installed (assuming MacOS/Linux): 要查看它的安装位置(假设是MacOS / Linux):

which eslint

Then see if it's working in Sublime Text (you may need to restart Sublime first). 然后看看它是否在Sublime Text中工作(您可能需要先重启Sublime)。 If it's not working, make sure in the eslint package settings that the path is correct. 如果它不起作用,请确保在eslint包设置中路径正确。

  1. To install eslint globally: npm install -g eslint 全局安装eslint: npm install -g eslint

    To install eslint in your project folder: npm install eslint --save-dev 要在项目文件夹中npm install eslint --save-devnpm install eslint --save-dev

  2. Add in package.json this script : "eslint": "eslint --ignore-path .gitignore ." package.json添加以下脚本: "eslint": "eslint --ignore-path .gitignore ."

  3. Create a file called .eslintrc and insert : 创建一个名为.eslintrc的文件并插入:

     { "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 就个人而言,我将此文件保存在我的js文件夹中

  4. Go to node_modules/.bin 转到node_modules/.bin

  5. Run : eslint --init or npm run eslint nameOfYourFile 运行: eslint --initnpm run eslint nameOfYourFile

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 假设您为编辑器安装了一个eslint插件,如果您有npm install -g eslint ,那么您可以为特定环境安装附加组件,例如npm install eslint-config-airbnb eslint-plugin-react eslint-plugin-jsx-a11y eslint-plugin-import -g (这是对纯JS和React的支持),你可以通过这种方式添加对nodejs的支持,在工作文件夹make .eslintrc文件中看起来像这样

{
    "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 . 如果您需要节点支持,那么在.eslintrc的env部分中将节点设置为'true'并使用下一个npm i eslint-plugin-node -g全局安装eslint-node插件。 Then in extends section of .eslintrc add "plugin:node/recommended" . 然后在.eslintrc的extends部分添加"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 . 通过这种方式,您将在计算机上具有.eslintrc文件的每个项目中获得eslint支持。在.eslintrc rules部分中需要您需要的规则。 Thats it. 而已。

Unfortunately, ESLint no longer recommends the use of Personal Configuration.不幸的是,ESLint 不再推荐使用个人配置。 Even if you have ESLint and other ESLint configuration files installed in the global scope, it will not read them correctly.即使你在全局 scope 中安装了 ESLint 和其他 ESLint 配置文件,它也不会正确读取它们。

https://eslint.org/docs/latest/user-guide/configuring/configuration-files#personal-configuration-files-deprecated 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.此功能将在 8.0.0 版本中删除。 If you want to continue to use personal configuration files, please use the --config CLI option .如果您想继续使用个人配置文件,请使用--config CLI 选项 For more information regarding this decision, please see RFC 28 and RFC 32 .有关此决定的更多信息,请参阅RFC 28RFC 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.这里提到的个人配置文件是~/.eslintrc.*文件,目前处理方式与其他配置文件不同。

How does ESLint find personal configuration files? ESLint 如何找到个人配置文件?

If eslint could not find any configuration file in the project, eslint loads ~/.eslintrc.* file.如果eslint在项目中找不到任何配置文件, eslint会加载~/.eslintrc.*文件。

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.如果eslint可以在项目中找到配置文件, eslint忽略~/.eslintrc.*文件,即使它位于项目目录的祖先目录中。

How do personal configuration files behave?个人配置文件的行为如何?

~/.eslintrc.* files behave similarly to regular configuration files, with some exceptions: ~/.eslintrc.*文件的行为类似于常规配置文件,但有一些例外:

~/.eslintrc.* files load shareable configs and custom parsers from ~/node_modules/ – similarly to require() – in the user's home directory. ~/.eslintrc.*文件从~/node_modules/加载可共享的配置和自定义解析器 - 类似于require() - 在用户的主目录中。 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. ~/.eslintrc.*文件默认从$CWD/node_modules加载插件,以便唯一识别插件。 If you want to use plugins with ~/.eslintrc.* files, plugins must be installed locally per project.如果您想使用带有~/.eslintrc.*文件的插件,则必须在每个项目本地安装插件。 Alternatively, you can use the --resolve-plugins-relative-to CLI option to change the location from which ESLint loads plugins.或者,您可以使用--resolve-plugins-relative-to CLI 选项来更改 ESLint 加载插件的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM