Detalhes do pacote

strict-mode

fibo1.6kMIT1.1.6

enables strict mode in your package

strict, use strict, strict mode

readme (leia-me)

strict-mode

enables strict mode in your package

No deps

Usage

Please notice that this package is intended to be used server-side and with CommonJS only.

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

  • require a big effort
  • be error-prone
  • make linters complain (like eslint, jshint, etc.)
  • be a problem when concatenating files

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

Use case

At the time of this writing, Node v4 stable version was released a 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 be raised. 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

You could use strict-mode just as a development dependency.

Following the instructions below, you are not going to deploy strict-mode to production, but when you run your tests, if some code is not strict, then you will get an error.

Suppose you have the following folder structure in your package.

.
├── package.json
├── src
│   └── index.js    # your package.json `main` entry
└── test
    ├── test1.js
    └── test2.js

Assuming your package name is, hmm, my-package, create a file test/my-package.js containing

require('strict-mode')(function () {
  module.exports = require('../src/index.js')
})

Now if you set the environment variable NODE_PATH=test, you can use require('my-package') in your tests.

For instance if you are using Node built-in Test runner you can add this to your package.json

    "test": "NODE_PATH=test node --test"

Credits

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

License

MIT