Using "use strict"
allows support for ES6 features, which Node 4.0 supports, in JavaScript code.
However, it is cumbersome to have this at the beginning of every single back-end code file.
Is there a way to configure Node to just assume "use strict"
is always in use within an application?
No, you can't disable strict mode per function. Notice how we can define function outside of strict code and then pass it into the function that's strict. You can do something similar in your example — have an object with "sloppy" functions, then pass that object to that strict immediately invoked function.
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.
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.
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.
You can pass the --use_strict
command line option to the node
command. This will treat all of your code as being in strict mode.
Alternately, you can use the package https://www.npmjs.com/package/use-strict. That way you don't have to give the command line argument each time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With