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

╄→尐↘猪︶ㄣ 提交于 2020-01-30 19:32:32

问题


I am new to ESLint, and I have successfully integrated ESLint with 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 .eslintrc at 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 .eslintrc works).

// 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:

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

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

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


回答1:


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

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

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

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

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




回答2:


Adding amd to env inside .eslintrc will enable you to use define() and require(), as per the amd spec:

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



回答3:


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

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



来源:https://stackoverflow.com/questions/30901417/eslint-how-to-set-eslintrc-to-recognize-require

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!