strict-mode

enables strict mode in your package

NPM version Build Status Dependency Status

js-standard-style

NPM

Installation

With npm do

npm install strict-mode

Usage

Please note that this package is intended to be used server side. If used with browserify it is a no op.

Suppose that the main attribute in your package.json is index.js. If you want that all the modules in your package have 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

})

Motivation

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

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

Use case

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 if 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
})

Bonus tip

Usually you write tests importing your library. You can do that in two ways:

In both cases you miss the feature provided by strict-mode, but, you can use this nasty trick ( to cheat npm :^)

Assuming you package name is, emh package-name, create a file test/node_modules/package-name/index.js containing

module.exports = require('../../..')

See for example test/node_modules/strict-mode/index.js used in this package.

Then you can use require('package-name') in your tests.

Credits

Code stolen from isaacs’ use-strict. A big thank to MDN cause it is an awesome knowledge base for everything related with the Web: in particular, I could find some valid counterexamples of strict mode to include in my tests.

License

MIT