How to set .eslintrc to recognize 'require'?
Solution 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
.
Solution 2
Adding amd
to env
inside .eslintrc
will enable you to use define()
and require()
, as per the amd
spec:
{
"env": {
"amd": true
}
}
Solution 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
Solution 4
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!)
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.
Dan Nissenbaum
Freelance C++ and PHP developer, with PhD in physics, in Brattleboro, VT, USA Generally with bare feet Best to all!
Updated on July 05, 2022Comments
-
Dan Nissenbaum almost 2 years
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 recognizesnode
(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 recognizerequire()
. 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 recognizerequire()
?(If, in your answer, you can provide insight how to cover more general cases, that would also be helpful. Thanks!)
-
Dan Nissenbaum almost 9 yearsHmm. I tried adding
"amd":true
to my.eslintrc
file, and no go. I'll play around with that some more. -
Nick Avi almost 9 yearsDid you restart sublime after adding? (hate to ask)
-
Nick Avi almost 9 years"env": { "amd": true }
-
Marko Gresak almost 9 years@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.
-
Nick Avi almost 9 yearsbah, i went off pic which i think looks like sublime
-
Dan Nissenbaum almost 9 yearsMust 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 theamd
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 almost 9 yearsThanks! 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 almost 9 yearsFor anyone interested, here is a link to a comparison between
JSLint
,JSHint
,JSCS
, andESLint
: sitepoint.com/comparison-javascript-linting-tools - from this link, I will go withESLint
. -
Marko Gresak almost 9 yearsIt'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, 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 almost 9 yearsIf 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.
-
michaelok over 6 yearsAs 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." jslint.com/help.html