node package manager

strict-mode

enables strict mode in your package

strict-mode

enables strict mode in your package

With npm do

npm install strict-mode

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

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

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

  • using require('../../path/to/my/module')
  • or setting NODE_PATH=src and using require('my/module')

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.

Code stolen from isaacs' use-strict.

MIT