Search code examples
javascriptnode.jsecmascript-6commonjs

Is it advisable to use `const` for CommonJS requires?


In CommonJS (Node in particular), the require() variables are usually only assigned once and never changed. So is it a good practice to use the ES6 const for these? As in:

const level = require('level');

instead of:

var level = require('level');

What kind of good/bad effects will it have?


Solution

  • It is, indeed, a good practice to use const for requires. Most current projects supporting Node 4+ do that, and there is virtually no scenario where you would want to modify a required module variable.