The future of (javascript) modules (in node)

The future of (javascript) modules (in node)

What's up with ES Modules and node anyway? What's the state of play in June 2018? How did we get here anyway?

0a356ffe066d3d8bf05be73fa57c0a44?s=128

C J Silverio

June 14, 2018
Tweet

Transcript

  1. 13.

    const chalk = require('chalk'); // cjs import chalk from 'chalk';

    // esm console.log(chalk.bold('hello world!'));
  2. 14.

    const { yellow, bold } = require('chalk'); // cjs import

    { yellow, bold } from 'chalk'; // esm console.log(`${bold('hello')} ${yellow('world!')}`);
  3. 15.

    function red(word) = { ... }; // etc module.exports =

    { red, green, blue, bold, ...}; // cjs export { red, green, blue, bold, ...}; // esm
  4. 18.
  5. 23.

    there was a lot of server-side js going on and

    people needed a way to share code
  6. 26.

    const chalk = require('chalk'); // loads './node_modules/chalk/index.js' const other =

    require('./chalk'); // loads './chalk.js' if it !nds it
  7. 29.
  8. 40.
  9. 53.
  10. 70.
  11. 73.
  12. 74.

    well, not very hard. Chris Dickinson & I implemented a

    usable take on ESM + CJS in node.
  13. 76.

    our aim was to prove with a bit of parsing

    you could get a lot of usability
  14. 78.
  15. 79.
  16. 87.
  17. 91.
  18. 92.

    if you are using babel in your build you have

    no reason not to use ESM now
  19. 99.

    npm audit & npm audit !x report and automatically fix

    known vulnerabilities in your dep tree
  20. 104.
  21. 106.

    require = require('esm')(module); const Widget = require('./main.js').default; // over in

    main.js import fs from 'fs'; import cjs from './cjs.js'; const esm = require('./esm.js'); export default class Widget { ... }
  22. 108.
  23. 117.