Any way to force strict mode in node?

node.js

node.js Problem Overview


Could not find this answer anywhere, but I did find several mailing lists where this was discussed, these are rather old however and I have no idea if this is implemented or not.

Is there anyway to force using strict mode in node.js?

Writing "use strict"; in all my .js files... well, i prefer it being forced to using strict mode, rather than adding extra boilerplate.

node.js Solutions


Solution 1 - node.js

According to Lloyd you can now place

"use strict";

at the top of your file in node >= 0.10.7, but if you want your whole app to run in strict (including external modules) you can do this

> node --use_strict

Solution 2 - node.js

In node 0.10.7 you can enforce strict mode at file level by placing "use strict"; at the top of your file. Finally!

Solution 3 - node.js

You can also use

https://npmjs.org/package/use-strict

that is, write once

require('use-strict')

or even take a step forward and use

https://npmjs.org/package/node-strict

Please note that use-strict will turn on strict more on every module required after invocation.

If you prefer a not invasive approach, I wrote another module

https://www.npmjs.org/package/strict-mode

which enables strict mode only in your package. I think that is more a "Do What I Mean" solution.

Solution 4 - node.js

Just use "use strict"; at the top of applicable files. I know it's tempting to try to cut out boilerplate, but it simply can not be done in Javascript. The node flag which shall not be named[1]

  • is undocumented, and unsupported by Node itself.
  • has faced proposals to remove it.
  • is node-specific and is not supported in any other JavaScript engine.
  • is unstandardized.
  • it is not the same as "use strict"; because it is a compiler global, and like all globals you're potentially adversely impacting someone else's code.
  • everything is subject to bugs. strict mode and sloppy-mode may be subject to different bugs. that is to say, some strict mode bugs are unique to strict mode

Some other programmers may think this is similar to -wALL or the like, it's not. This is standardized functionality that you're enabling in an ad-hoc fashion (breaking the standard) and changing everyone's compiler semantics.

Footnotes

  1. The node flag is --use_strict. Don't use it.

Solution 5 - node.js

You can also provide the strict flag on the shebang interpreter directive.

#!/usr/bin/env node --use_strict

But currently (at least pre v0.9.x) it suffers the same problems described by the comments in @chad-scira's answer discuss.

Solution 6 - node.js

It's worth noting that ESLint enforces strict mode by default. It won't stop you from running node on a file that doesn't adhere to strict mode of course, but if you have ESLint as a required part of your build and CI process, then developers will only be able to commit strict-mode code.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobin Heggelund HansenView Question on Stackoverflow
Solution 1 - node.jsChad SciraView Answer on Stackoverflow
Solution 2 - node.jsLloydView Answer on Stackoverflow
Solution 3 - node.jsGianluca CasatiView Answer on Stackoverflow
Solution 4 - node.jsEvan CarrollView Answer on Stackoverflow
Solution 5 - node.jsJohn LehmannView Answer on Stackoverflow
Solution 6 - node.jsMatt BrowneView Answer on Stackoverflow