enables strict mode in your package


enables strict mode in your package

With npm do

npm install strict-mode

Suppose that the main attribute in your package.json is index.js.

If you want that all the modules in your package has strict mode enabled, just wrap your index.js this way

require('strict-mode')(function () {
// your index.js content 
// every *require* call inside this function will have strict mode enabled 

Please note that this package is intended to be used server side. If used with browserify it is a no op (test.html here). I could not find a way to extend this feature client side: hints and suggestions are welcome, contact me if any.

Strict mode is a best practice but adding a "use strict"; on top of every .js file in your package could

  • require a big effort
  • be error proning
  • make complain jshint
  • be a problem when concatenating files

On the other hand the use-strict package solution is too invasive, cause it applies strictness to all future modules loaded.

At the time of this writing Node v4 stable version was released few days ago. Finally we can use class, let, const (among other new exciting features) but you will notice that it you do not turn on strict mode an exception will raise. For instance, a file Point2d.js with content

class Point2d {
  constructor (x, y) {
    this.x = x
    this.y = y
module.exports = Point2d

when imported will complain

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

but if you wrap the import with strict-mode everything will just work

require('strict-mode')(function () {
  var Point2d = require('./Point2d')
  // require all other classes you need.
  // You can also export them safely
  exports.Point2d = Point2d

Code stolen from isaacs' use-strict.