配置全局安装的 eslint
Configuring globally installed eslint
我全局安装了 eslint@4.2.0 并想使用一些预定义的配置。当我尝试安装 eslint-config-google
时,它说我没有 eslint>=4.1.0(我当然有,但安装了全局)。安装 eslint-config-airbnb-base
时出现同样的问题 - 最终预定义的配置无法看到全局安装的 eslint(甚至 eslint --init
也看不到它,因为它在我 运行 时在本地安装了另一个 eslint 实例它)。然后我决定在本地安装 eslint 和配置文件,在我的项目目录中 - 工作起来很有魅力,但我仍然希望在全球范围内拥有这些东西,这样我每次制作时都不必关心它我的项目的新目录。
是否可以使 eslint 和预定义配置在全局范围内工作,或者至少具有全局 eslint 和本地配置文件?
ultimately predefined configs can't see globally installed eslint
事实上,他们可以。或者更确切地说,全局 ESLint 可以使用全局配置。它只是不知道如何跨越全局和本地之间的障碍。
Then I decided to install both eslint and configuration files locally, in my projects directory - works like a charm
不错的选择。当您最终拥有具有冲突需求的项目时,这将是至关重要的。
but still I'd like to have these things in global scope so I wouldn't have to care about it each time I make a new directory for my projects
这就是像Yeoman are for. Here is the one I use, generator-seth这样的项目脚手架工具。
每当我开始一个新项目时,我只需输入 seth
并回答几个问题,就可以在后台使用光荣的魔法完成。
Is it possible to make eslint and predifined configs work at global scope or at least have global eslint and local configuration file?
是的。你可以有一个本地 .eslintrc
文件,全局 ESLint 会尊重它。您可以 运行 eslint --init
进行设置(这也会在本地安装 ESLint,但您不需要使用它)。
但更好的方法是使用 XO,它建立在 ESLint 之上(并支持所有 ESLint 规则)。与 ESLint 不同,XO 知道如何自动选择合适的安装来使用。您可以在本地和全局安装它,当检测到一个时,它的全局 CLI 将遵从本地副本。这很重要,因为这意味着每个项目都可以保留自己的 linter 版本,同时仍然允许您在全局范围内 运行 XO。因此,如果出现重大变更,您的项目不会一下子搞砸或在需求冲突时被破坏。 XO 的配置以任何一种方式保存在本地,因此与 ESLint 不同,您没有重复的本地和全局配置。
我最近遇到了同样的问题,这是我的解决方案(OS:Windows):
首先,我运行 eslint 初始命令来配置我的首选项:
$ eslint --init
? How would you like to use ESLint? To check syntax, find problems, and enforce code style
? What type of modules does your project use? CommonJS (require/exports)
? Which framework does your project use? None of these
? Where does your code run? Browser, Node
? How would you like to define a style for your project? Use a popular style guide
? Which style guide do you want to follow? Airbnb (https://github.com/airbnb/javascript)
? What format do you want your config file to be in? JavaScript
Checking peerDependencies of eslint-config-airbnb-base@latest
The config that you've selected requires the following dependencies:
eslint-config-airbnb-base@latest eslint@^4.19.1 || ^5.3.0 eslint-plugin-import@^2.17.2
? Would you like to install them now with npm? No
其次,我将eslint本地生成的“.eslintrc.js”文件复制到我的用户主目录(C:\Users\username)中进行制作全局配置文件。
最后,我使用 npm 安装所需的包 globally:
npm install -g eslint@^5.16.0 eslint-config-airbnb-base eslint-plugin-import
*Important*
Don't use npm install -g eslint
because it will install the latest eslint version
(like 6.0.1) and therefore, the dependencies of eslint-config-airbnb-base and eslint-plugin-import will go wrong
这里是这篇文章,如果您想要不同的配置,您可以找到更多信息How to globally set up eslint in vscode。
我全局安装了 eslint@4.2.0 并想使用一些预定义的配置。当我尝试安装 eslint-config-google
时,它说我没有 eslint>=4.1.0(我当然有,但安装了全局)。安装 eslint-config-airbnb-base
时出现同样的问题 - 最终预定义的配置无法看到全局安装的 eslint(甚至 eslint --init
也看不到它,因为它在我 运行 时在本地安装了另一个 eslint 实例它)。然后我决定在本地安装 eslint 和配置文件,在我的项目目录中 - 工作起来很有魅力,但我仍然希望在全球范围内拥有这些东西,这样我每次制作时都不必关心它我的项目的新目录。
是否可以使 eslint 和预定义配置在全局范围内工作,或者至少具有全局 eslint 和本地配置文件?
ultimately predefined configs can't see globally installed eslint
事实上,他们可以。或者更确切地说,全局 ESLint 可以使用全局配置。它只是不知道如何跨越全局和本地之间的障碍。
Then I decided to install both eslint and configuration files locally, in my projects directory - works like a charm
不错的选择。当您最终拥有具有冲突需求的项目时,这将是至关重要的。
but still I'd like to have these things in global scope so I wouldn't have to care about it each time I make a new directory for my projects
这就是像Yeoman are for. Here is the one I use, generator-seth这样的项目脚手架工具。
每当我开始一个新项目时,我只需输入 seth
并回答几个问题,就可以在后台使用光荣的魔法完成。
Is it possible to make eslint and predifined configs work at global scope or at least have global eslint and local configuration file?
是的。你可以有一个本地 .eslintrc
文件,全局 ESLint 会尊重它。您可以 运行 eslint --init
进行设置(这也会在本地安装 ESLint,但您不需要使用它)。
但更好的方法是使用 XO,它建立在 ESLint 之上(并支持所有 ESLint 规则)。与 ESLint 不同,XO 知道如何自动选择合适的安装来使用。您可以在本地和全局安装它,当检测到一个时,它的全局 CLI 将遵从本地副本。这很重要,因为这意味着每个项目都可以保留自己的 linter 版本,同时仍然允许您在全局范围内 运行 XO。因此,如果出现重大变更,您的项目不会一下子搞砸或在需求冲突时被破坏。 XO 的配置以任何一种方式保存在本地,因此与 ESLint 不同,您没有重复的本地和全局配置。
我最近遇到了同样的问题,这是我的解决方案(OS:Windows):
首先,我运行 eslint 初始命令来配置我的首选项:
$ eslint --init
? How would you like to use ESLint? To check syntax, find problems, and enforce code style
? What type of modules does your project use? CommonJS (require/exports)
? Which framework does your project use? None of these
? Where does your code run? Browser, Node
? How would you like to define a style for your project? Use a popular style guide
? Which style guide do you want to follow? Airbnb (https://github.com/airbnb/javascript)
? What format do you want your config file to be in? JavaScript
Checking peerDependencies of eslint-config-airbnb-base@latest
The config that you've selected requires the following dependencies:eslint-config-airbnb-base@latest eslint@^4.19.1 || ^5.3.0 eslint-plugin-import@^2.17.2
? Would you like to install them now with npm? No
其次,我将eslint本地生成的“.eslintrc.js”文件复制到我的用户主目录(C:\Users\username)中进行制作全局配置文件。
最后,我使用 npm 安装所需的包 globally:
npm install -g eslint@^5.16.0 eslint-config-airbnb-base eslint-plugin-import
*Important*
Don't use
npm install -g eslint
because it will install the latest eslint version (like 6.0.1) and therefore, the dependencies of eslint-config-airbnb-base and eslint-plugin-import will go wrong
这里是这篇文章,如果您想要不同的配置,您可以找到更多信息How to globally set up eslint in vscode。