Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set .eslintrc to recognize 'require'?

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:

ESLint does not recognize <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 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!)

like image 600
Dan Nissenbaum Avatar asked Jun 17 '15 20:06

Dan Nissenbaum


People also ask

How do I configure Eslintrc?

There are two primary ways to configure ESLint: Configuration Comments - use JavaScript comments to embed configuration information directly into a file. Configuration Files - use a JavaScript, JSON, or YAML file to specify configuration information for an entire directory and all of its subdirectories.


4 Answers

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

{
  "env": {
    "amd": true
  }
}
like image 93
Nick Avi Avatar answered Oct 11 '22 23:10

Nick Avi


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.

like image 33
Marko Gresak Avatar answered Oct 11 '22 23:10

Marko Gresak


"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

like image 4
Fabio Marasco Avatar answered Oct 12 '22 00:10

Fabio Marasco


On a Mac ... global solution. (2021)

If you are using the amazing ESLint in the amazing VS Code on Mac,

Simply go to ~ (ie /users/your-name)

edit .eslintrc.json (you can edit it in VSCode of course!)

enter image description here

You'll likely add

"node": true

if you're working with node, or perhaps "amd" as stated in the answers here. ("amd" gives specifically and only require and define).

This is a global solution for all workspaces you open.

Importantly, this also works if you are using VS Code "remotely", so, with no workspace. For example, you may open a file on a server just using sftp, and work on the file in VSCode. Or you may be opening just a single local file on the Mac, not part of a workspace. In both these cases the setting (eg, node=true) will in fact work - it needn't be a workspace.

like image 1
Fattie Avatar answered Oct 12 '22 00:10

Fattie