Files
blog/blog/ES6vsCJS.md
2020-06-03 02:24:48 +01:00

1.1 KiB

ES6 vs CommonJS

The differences in using import/export between the two:

Differences

  • Node is compatible with ES6. You can find a compatability table here: https://node.green.

CommonJS

To export functions use exports.name:

exports.run = run;

You can also use:

module.exports = {
    run: run
}
```

for multiple exports.

To import functions use `require`. You can use object destructuring as well:

```javascript
const { run } = require("./build-lunrjs-index");
const gulp = require("gulp");

If using destructuring you can then run the function as if it were local:

function buildSearch(cb) {
  run();
  cb();
}

If you don't use destructuring (const run = require("./build-lunrjs-index");) then you have to pass the constant name and the export you want in:

function buildSearch(cb) {
  run.run();
  cb();
}

Node and Gulp

If you are using node and or gulp you should use CJS for your module import and exports. You can use any javascript that node is compatible with in your code.