Javascript ESLint:如何设置 .eslintrc 以识别“需要”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30901417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:54:47  来源:igfitidea点击:

ESLint: How to set .eslintrc to recognize 'require'?

javascriptintellij-ideaeslintjshintlint

提问by Dan Nissenbaum

I am new to ESLint, and I have successfully integrated ESLint with IntelliJ.

我是ESLint 的新手,我已经成功地将 ESLint 与IntelliJ集成在一起。

Out of the box, my integration of ESLint did not recognize node, but basic review of documentation made clear that by creating the configuration file named .eslintrcat the root of my project folder (with the proper IntelliJ setting to access this file) and setting "node":true, ESLint recognizes node(i.e., the following complete .eslintrcworks).

开箱即用,我的 ESLint 集成无法识别node,但对文档的基本表明,通过创建在.eslintrc我的项目文件夹的根目录命名的配置文件(使用适当的 IntelliJ 设置来访问此文件)和设置"node":true,ESLint 可以识别node(即以下全集.eslintrc)。

// Contents of .eslintrc at root of project - support for Node and jQuery
{
  "env" : {
    "node" : true,
    "jquery" : true
  },
}

However, ESLint still does not recognize require(), as evidenced by this screenshot:

但是,ESLint 仍然无法识别require(),如下图所示:

ESLint does not recognize <code>require()</code>

ESLint 无法识别 <code>require()</code>

I have done my best in a reasonable amount of time searching for a solution to the basic question of how to get ESLintto recognize require(). In particular, I found a possible hint here, where it suggested to add "amd":falsein (I presumed) the .eslintrcfile - but no go.

我已经在合理的时间内尽我最大的努力寻找解决如何ESLint识别的基本问题的方法require()。特别是,我在这里找到了一个可能的提示,它建议添加"amd":false(我假设)该.eslintrc文件 - 但没有去。

This seems basic. How can I get .eslintrcto recognize require()?

这看起来很基本。我怎样.eslintrc才能认出require()

(If, in your answer, you can provide insight how to cover more general cases, that would also be helpful. Thanks!)

(如果在您的回答中,您可以提供有关如何涵盖更一般情况的见解,那也会有所帮助。谢谢!)

采纳答案by Marko Gre?ak

The problem is not with ESLint. If you look closely at your message, it says JSHint.

问题不在于 ESLint。如果您仔细查看您的消息,它会显示JSHint

Since you're trying to configure ESLint, simplest solution would be to disable or remove JSHint plugin form your IDE.

由于您正在尝试配置 ESLint,最简单的解决方案是从您的 IDE 中禁用或删除 JSHint 插件。

If you still want to use JSHint along with ESLint, you can do the following:

如果您仍然想将 JSHint 与 ESLint 一起使用,您可以执行以下操作:

Single file solution: add /* global require */at the top of your file.

单文件解决方案:/* global require */在文件顶部添加。

General solution for all files: add "node": trueline to your .jshintrc.

所有文件的通用解决方案:"node": true.jshintrc.

回答by Nick Avi

Adding amdto envinside .eslintrcwill enable you to use define()and require(), as per the amdspec:

根据规范,添加amdenvinside.eslintrc将使您能够使用define()and :require()amd

{
  "env": {
    "amd": true
  }
}

回答by Fabio Marasco

"amd":true in envdefines require() and define() as global variables as per the amd spec.

env 中的"amd":true 根据 amd 规范将 require() 和 define() 定义为全局变量。

See http://eslint.org/docs/user-guide/configuring#specifying-environments

请参阅http://eslint.org/docs/user-guide/configuring#specifying-environments