155

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.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
Robin Heggelund Hansen
  • 4,906
  • 6
  • 37
  • 54
  • I would highly suggest checking out [this much updated answer (rather than the dangerous answer which was chosen)](https://stackoverflow.com/a/51970329/124486) – Evan Carroll Sep 07 '20 at 00:45

7 Answers7

222

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

Chad Cache
  • 9,668
  • 3
  • 56
  • 48
  • eek: fs.js:1497 function emit() { ^^^^^^^^ SyntaxError: In strict mode code, functions can only be declared at top level or immediately within another function. – j03m Jan 17 '13 at 19:53
  • one of the modules you're using is not following the strict rules – Chad Cache Jan 18 '13 at 19:30
  • 1
    oh O_o, so you cant use the filesystem if you force strict? which version of node are you using, im not getting this on v0.8.12 – Chad Cache Jan 18 '13 at 22:38
  • 1
    'v0.8.16' <--- hmm odd. Let me go verify. – j03m Jan 22 '13 at 16:36
  • 01734a-j03m:node-limiter j03m$ node --use_strict test/exampleLimiter.js fs.js:1497 function emit() { ^^^^^^^^ [REDACTED] var vows = require('vows'), assert = require('assert'); var Limiter = require('../limiter.js'); var namespace = 'vowstest'; var mockUid = '1234556'; var Membase = require('memcached'); var m = new Membase("127.0.0.1:11211"); – j03m Jan 22 '13 at 16:38
  • that's of course using vows, assert and memcached, but it's definitely thrown in fs. – j03m Jan 22 '13 at 16:39
  • 13
    In node v0.9.x and newer the `--use_strict` flag works as expected. – TooTallNate Feb 05 '13 at 05:52
  • in node v0.8 with `--use_strict` I got `ReferenceError: global is not defined` in `node.js` itself – vincentlcy Jun 09 '13 at 16:50
  • 28
    Note that `--use_strict` will set strict mode to the whole application, include all exteneral modules, which are out of your controls. – Lewis Feb 05 '15 at 14:58
60

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

Lloyd
  • 8,204
  • 2
  • 38
  • 53
33

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.
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
27

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.

Gianluca Casati
  • 3,303
  • 1
  • 32
  • 20
20

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.

John Lehmann
  • 7,975
  • 4
  • 58
  • 71
2

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.

Matt Browne
  • 12,169
  • 4
  • 59
  • 75
1

If you're using CommonJS (NodeJS uses CommonJS modularization by default) there's no strict mode unless you include "use strict" in the file or execute node with the --use_strict flag

But in ECMAScript modules, strict mode is by default. You can enable it adding "type": "module" to package.json

i474232898
  • 781
  • 14
  • 25