Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Skip Using "use strict" In Node.js 4.0+?

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?

like image 636
Code Whisperer Avatar asked Jun 02 '16 18:06

Code Whisperer


People also ask

How do I get rid of use strict?

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?

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.

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.

Is there anyway to force using strict mode in 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.


1 Answers

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.

like image 150
Joe Attardi Avatar answered Sep 29 '22 05:09

Joe Attardi