Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js and eslint disagree on "use strict"

ESLint tells me that I do not need "use strict" at the top of my index.js file (it's a simple server like the 6-line one on https://nodejs.org/en/about/). Apparently all node modules are already in strict mode. Makes sense.

However, running node index.js gets me a "SyntaxError: [let] not supported outside strict mode." does run with the "redundant" "use strict" pragma.

Why the inconsistency? Shouldn't node know that this node module is indeed strict by default? Could this be due to some simple misconfiguration of node, ESLint, or my IDE?

like image 920
ivanjonas Avatar asked Sep 25 '15 22:09

ivanjonas


People also ask

Is use strict still needed?

Is use strict necessary? The strict mode is no longer required since the release of ES2015, which fixes most of JavaScript's confusing behavior with a more robust syntax. It's just good to know because you might find it in legacy projects that still used it.

Should I use strict mode JS?

Strict mode makes several changes to JavaScript semantics. It eliminates silent errors and instead throws them so that the code won't run with errors in the code. It will also point out mistakes that prevent JavaScript engines from doing optimizations.

Does node need use strict?

The default NodeJS set up, when you initialize your project first for example with the npm init command does not use strict mode. NodeJS uses CommonJS modularization by default. So if you hang up with this configuration, you end up not using strict mode in any of your files.


1 Answers

ESLint makes its own decisions about what it considers to be valid or invalid warnings or errors. You have to treat everything that eslint/jslint/jshint says as advisory on top of everything else. According to someone somewhere their suggestions are optimal and perfectly valid.

That being said, you do have some options to suppress this specific warning:

  • Use eslint flags in comments in the code
  • Run eslint with configuration to specify this flag
  • Use the --use-strict flag when running node

The specific reason about why you get this warning has to do with the fact that the default node interpreter as it stands is not fully ES6-ready. For example, in node 4 you cannot use let outside of strict mode even though let is an ES6 keyword.

like image 198
Explosion Pills Avatar answered Oct 04 '22 03:10

Explosion Pills