73

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!)

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
Dan Nissenbaum
  • 13,558
  • 21
  • 105
  • 181

4 Answers4

103

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

{
  "env": {
    "amd": true
  }
}
Daniel
  • 3,115
  • 5
  • 28
  • 39
Nick Avi
  • 1,231
  • 2
  • 7
  • 6
  • 1
    Hmm. I tried adding `"amd":true` to my `.eslintrc` file, and no go. I'll play around with that some more. – Dan Nissenbaum Jun 17 '15 at 20:12
  • Did you restart sublime after adding? (hate to ask) – Nick Avi Jun 17 '15 at 20:17
  • "env": { "amd": true } – Nick Avi Jun 17 '15 at 20:19
  • @NickAvi why would you assume OP is using Sublime? They clearly said it's IntellJ, not to mention interface alone doesn't look anything like Sublime. – Marko Gresak Jun 17 '15 at 20:19
  • 1
    bah, i went off pic which i think looks like sublime – Nick Avi Jun 17 '15 at 20:20
  • Must be an issue with Intellij and/or the path to my `.eslintrc` not being picked up. I have `"amd":false` and I've restarted IntelliJ. No go. But now I know that the `amd` solution really is supposed to take effect, so I'll keep playing around with the IntelliJ/ESLint settings to get it to access my `.eslintrc` file. Thanks for the help. – Dan Nissenbaum Jun 17 '15 at 20:22
42

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.

Marko Gresak
  • 7,950
  • 5
  • 40
  • 46
  • 1
    Thanks! It was only on the tip of my subconscious that these are different. Would you offhand have a recommendation about which is 'better'? JSHint, ESLint... or is it beneficial to use both? – Dan Nissenbaum Jun 17 '15 at 22:55
  • For anyone interested, here is a link to a comparison between `JSLint`, `JSHint`, `JSCS`, and `ESLint`: http://www.sitepoint.com/comparison-javascript-linting-tools/ - from this link, I will go with `ESLint`. – Dan Nissenbaum Jun 17 '15 at 23:02
  • 1
    It's hard to say. For what little JS I write (I'm mostly using TypeScript now), I still use JSHint, because ESLint didn't exist at the time when I was using pure JS. As [ESLint FAQ says](https://github.com/eslint/eslint#im-not-giving-up-jshint-for-this), the one is not better from the other. Things which you should consider: ESLint has full ES6 support, which I can't confirm for JSHint. ESLint also supports a lot of code style rules (similar to JSCS) and JSHint is explicitly dropping these rules and suggests use of JSCS for these checks, which makes some sense, but you need to use 2 tools. – Marko Gresak Jun 17 '15 at 23:07
  • 2
    If you're planning on writing ES6 code, ESLint would probably be a better option, because from what I learned in quick google query, JSHint still doesn't offer great support -- *I might be wrong on this*. It's also great to force you into having some basic code style, but still doesn't add all the complex rules that come with JSCS, if you care about that at all. If I were choosing today, I would probably go with ESLint because it's more up to date and would suit my use cases better. Sorry for these long comments, I hope this will help you with choosing your lint tool. – Marko Gresak Jun 17 '15 at 23:07
  • As far as the question on ESLint and ES6 support, I'll agree w/ your comments especially given this statement on the lint site: "Some of ES6’s features are good, so JSLint will recognize the good parts of ES6." http://www.jslint.com/help.html – michaelok Feb 14 '18 at 23:13
4

"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

1

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.

Fattie
  • 27,874
  • 70
  • 431
  • 719