Skip to content

Instantly share code, notes, and snippets.

@medikoo
Last active February 17, 2017 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save medikoo/0790e566e769231c0b928a2bdb82fc02 to your computer and use it in GitHub Desktop.
Save medikoo/0790e566e769231c0b928a2bdb82fc02 to your computer and use it in GitHub Desktop.
ES modules in Node.js (basic idea)
// Import CJS module
const otherCJSModule = require('./other-cjs-module');
// Import ES module, relying on upcoming standard
import('./es-module').then(function (esModule) {
...
});
// Import core module
const fs = require('fs');
// Import ES module
import * from './other-es-module';
// Import CJS module
// This may need to look a bit different, as 'require' is expected to be different function
// in each module that imports it
import { require } from 'nodejs';
const cjsModule = require('./cjs-module');
// Import core module
import fs from 'fs';

Run CJS module

node cjs-module.js

Run ES module

node --esm es-module.js

Or introduce other binary:

nodejs es-module.js

(Possibly with some major Node.js version ES module could start being default)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment