简体   繁体   English

配置全局安装的eslint

[英]Configuring globally installed eslint

I installed globally eslint@4.2.0 and wanted to use some predefined configs. 我全局安装了eslint@4.2.0,并希望使用一些预定义的配置。 When I tried to install eslint-config-google it said that I don't have eslint>=4.1.0 (which I had of course, but installed globaly). 当我尝试安装eslint-config-google它说我没有eslint> = 4.1.0(我当然有,但是安装了globaly)。 The same problem occured when installing eslint-config-airbnb-base - ultimately predefined configs can't see globally installed eslint (and even eslint --init can't see it cause it installed another instance of eslint locally when I run it). 安装eslint-config-airbnb-base时发生了相同的问题-最终预定义的配置看不到全局安装的eslint(甚至eslint --init也看不到它导致它在我运行时在本地安装了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. 然后,我决定在我的项目目录中本地安装eslint和配置文件-就像一个超级按钮一样工作,但是我仍然希望将这些东西放置在全局范围内,因此每次我制作一个我的项目的新目录。 Is it possible to make eslint and predifined configs work at global scope or at least have global eslint and local configuration file? 是否可以使eslint和预定义的配置在全局范围内工作,或者至少具有全局eslint和本地配置文件?

ultimately predefined configs can't see globally installed eslint 最终预定义的配置看不到全局安装的eslint

Actually, they can. 实际上,他们可以。 Or rather, global ESLint can use global configs. 更确切地说,全局ESLint可以使用全局配置。 It just doesn't know how to cross the barrier between global and local. 它只是不知道如何跨越全球和本地之间的障碍。

Then I decided to install both eslint and configuration files locally, in my projects directory - works like a charm 然后,我决定在我的项目目录中本地安装eslint和配置文件-就像一个魅力

Good choice. 好的选择。 This is going to be crucial when you eventually have projects with conflicting needs. 当您最终有需求冲突的项目时,这将至关重要。

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 但是我仍然希望将这些东西放在全球范围内,这样我每次为项目创建新目录时都不必关心它

That is what project scaffolding tools like Yeoman are for. 这就是Yeoman等项目脚手架工具的用途。 Here is the one I use, generator-seth . 这是我使用的那个generator-seth

Whenever I start a new project, I simply type seth and answer a couple of questions and it's done with glorious magic under the hood. 每当我开始一个新项目时,我只要键入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? 是否可以使eslint和预定义的配置在全局范围内工作,或者至少具有全局eslint和本地配置文件?

Yepp. 是的 You can have a local .eslintrc file and the global ESLint will respect it. 您可以有一个本地.eslintrc文件,全局ESLint将尊重该文件。 You can run eslint --init to set this up (that will also install ESLint locally, but you don't need to use it). 您可以运行eslint --init进行设置(这也会在本地安装ESLint,但是您无需使用它)。

But an even better way is to use XO , which is built on top of ESLint (and supports all ESLint rules). 但是更好的方法是使用XO ,它建立在ESLint之上(并支持所有ESLint规则)。 Unlike ESLint, XO knows how to automatically choose the appropriate installation to use. 与ESLint不同,XO知道如何自动选择要使用的适当安装。 You can install it both locally and globally and its global CLI will defer to the local copy when one is detected. 您可以在本地和全局安装它,并且在检测到它时,其全局CLI将遵循本地副本。 This is important because it means that each project can keep its own version of the linter while still allowing you to run XO globally. 这很重要,因为这意味着每个项目可以保留自己的linter版本,同时仍然允许您全局运行XO。 So if a breaking change comes out, your projects are not screwed all at once or broken when they have conflicting needs. 因此,如果出现重大变化,您的项目不会立即陷入困境,也不会在有冲突的需求时中断。 The configuration for XO is kept locally either way, so unlike ESLint you do not have duplicate local and global configs. XO的配置都以本地方式保留,因此与ESLint不同,您没有重复的本地和全局配置。

I have recently encountered the same issue, here is my solution (OS: Windows): 我最近遇到了同样的问题,这是我的解决方案(操作系统:Windows):

First, I run eslint initial command to configure my preference: 首先,我运行eslint initial命令来配置我的首选项:

$ eslint --init

? How would you like to use ESLint? 您想如何使用ESLint? To check syntax, find problems, and enforce code style 要检查语法,查找问题并强制执行代码样式
? What type of modules does your project use? 您的项目使用什么类型的模块? CommonJS (require/exports) CommonJS(需求/出口)
? 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 ) Airbnb( https://github.com/airbnb/javascript
? What format do you want your config file to be in? 您希望配置文件采用哪种格式? JavaScript 的JavaScript
Checking peerDependencies of eslint-config-airbnb-base@latest 检查eslint-config-airbnb-base @ latest的peerDependencies
The config that you've selected requires the following dependencies: 您选择的配置需要以下依赖项:

eslint-config-airbnb-base@latest eslint@^4.19.1 || eslint-config-airbnb-base @ latest eslint@^4.19.1 || ^5.3.0 eslint-plugin-import@^2.17.2 ^ 5.3.0 eslint-plugin-import@^2.17.2
? Would you like to install them now with npm? 您要立即使用npm安装它们吗? No 没有

Second, I copy the " .eslintrc.js " file that eslint generated locally into my user home directory (C:\\Users\\username) to make it a global configuration file. 其次,我将本地生成的eslint的“ .eslintrc.js ”文件复制到我的用户主目录(C:\\ Users \\ username)中,以使其成为全局配置文件。

Finally, I use npm to install the required packages globally : 最后,我使用npm 全局安装所需的软件包:

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 不要使用npm install -g eslint因为它将安装最新的eslint版本(如6.0.1),因此, eslint-config-airbnb-baseeslint-plugin-import的依赖关系会出错

And here is the article you may find more information if you want configure differently How to globally set up eslint in vscode . 如果您希望以不同的方式进行配置,可以在这里找到更多信息。 如何在vscode中全局设置eslint

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

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