Upgrade to Pro — share decks privately, control downloads, hide ads and more …

To ESM is human: The journeyman's guide to ES Modules in Node.js

Ujjwal Sharma
September 19, 2019

To ESM is human: The journeyman's guide to ES Modules in Node.js

There’s only one thing more complicated than ES Modules on browsers – ES Modules in Node.js. But how did we get here? How do modules even work? What are the merits and demerits around these conflicting module systems and why does everyone get so emotional while talking about them?

Join me, a Node.js core collaborator and an avid contributor to V8 in order to know all these and more. You will come away with a deep understanding of how the module systems work internally in Node.js and what are the challenges facing these systems, moving forwards.

Ujjwal Sharma

September 19, 2019
Tweet

Other Decks in Technology

Transcript

  1. To ESM is human The Journeyman’s Guide to Modules in

    Node.js 2 Ujjwal Sharma @ryzokuken
  2. Ujjwal Sharma (@ryzokuken) •Igalia - CE Student (Compilers Group) •Node.js

    – Core Collaborator •Electron – Maintainer (Upgrades WG) •V8 and TC39 Contributor •Google Summer of Code •Student •Speaker !3 @ryzokuken
  3. DISCLAIMER •The cake is a lie. •This presentation is purely

    educational. •There are no grand discoveries, no revelations, no clever innovations. •Just systems, trusty old systems. !4
  4. !6

  5. !7

  6. – Lin Clark “Many JavaScript developers know that ES modules

    have been controversial. But few actually understand how ES modules work.” !8
  7. !10

  8. !11

  9. !12

  10. QUESTIONS 1. What are ES Modules? 2. What do they

    achieve? Why are they necessary? 3. What are the issues with ESM? How are they being resolved/mitigated? 4. What are our alternatives? !13
  11. Benefits: • Basic Modularity • (Less) Namespace Pollution Drawbacks: •

    No Dependency Resolution • Namespace Pollution • Messy Syntax? !29 @ryzokuken
  12. !30

  13. Module Authors: But how do I enable people to use

    my module without worrying about which format? !50 @ryzokuken
  14. Isolate A VM instance with its own heap. Chrome V8

    Node.js V8 Chrome Node.js V8 Electron !63 @ryzokuken
  15. Q: What’s the job of the Isolate? A: Heap allocation

    and garbage collection, mainly. !64 @ryzokuken
  16. C++ Stack Isolate Heap JS Land Object Object Object ???

    Handle Scope Pointer? Handle Handle Handle Handle !66 @ryzokuken
  17. Context A sandboxed execution context with its own set of

    built-in objects and functions. Isolate Context Context JavaScript Code !67 @ryzokuken
  18. Process Thread Thread Thread Worker Worker Worker libuv V8 libuv

    V8 libuv V8 Event Loop Isolate Event Loop Isolate Event Loop Isolate Heap+GC Heap+GC Heap+GC Node.js Environment Node.js Environment Node.js Environment node stdlib node stdlib node stdlib Context Context Context Context Context Context Context Context Context JS builtins JS builtins JS builtins JS builtins JS builtins JS builtins JS builtins JS builtins JS builtins !68 @ryzokuken
  19. !77 1.filename := Module._resolveFilename(…) 2.cachedModule := Module._cache[filename] 3.if cachedModule: return

    cachedModule.exports 4.[if nativeModule: return mod.compileForPublicLoader(...)] @ryzokuken
  20. !78 1.filename := Module._resolveFilename(…) 2.cachedModule := Module._cache[filename] 3.if cachedModule: return

    cachedModule.exports 4.[if nativeModule: return mod.compileForPublicLoader(...)] 5.module := new Module(filename, …) @ryzokuken
  21. !79 1.filename := Module._resolveFilename(…) 2.cachedModule := Module._cache[filename] 3.if cachedModule: return

    cachedModule.exports 4.[if nativeModule: return mod.compileForPublicLoader(...)] 5.module := new Module(filename, …) 6.Module._cache[filename] = module @ryzokuken
  22. !80 1.filename := Module._resolveFilename(…) 2.cachedModule := Module._cache[filename] 3.if cachedModule: return

    cachedModule.exports 4.[if nativeModule: return mod.compileForPublicLoader(...)] 5.module := new Module(filename, …) 6.Module._cache[filename] = module 7.module.load(filename) 8.if throw, delete from cache 9.otherwise return module.exports @ryzokuken
  23. !81 1.filename := Module._resolveFilename(…) 2.cachedModule := Module._cache[filename] 3.if cachedModule: return

    cachedModule.exports 4.[if nativeModule: return mod.compileForPublicLoader(...)] 5.module := new Module(filename, …) 6.Module._cache[filename] = module 7.module.load(filename) 8.if throw, delete from cache 9.otherwise return module.exports @ryzokuken
  24. !90 1.this.resolve(…args) 2.job := this.moduleMap.get(url) 3.If job: return job 4.job

    = new ModuleJob(…) 5.moduleMap.set(url, job) 6.return job @ryzokuken
  25. Problems •How to detect if a module is ESM or

    CommonJS? • How to import CommonJS modules from ESM? • How to import ESM modules from CommonJS? !93 @ryzokuken
  26. Special Thanks Myles Borins (@MylesBorins) Anna (@addaleax) Shelley Vohr (@codebytere)

    Dmitry Makhnev (@DmitryMakhnev) The Organizers !96 @ryzokuken