@@ -246,6 +246,11 @@ syncBuiltinESMExports();
246246fs .readFileSync === readFileSync;
247247```
248248
249+ ## ` import() ` expressions
250+
251+ [ Dynamic ` import() ` ] [ ] is supported in both CommonJS and ES modules. In CommonJS
252+ modules it can be used to load ES modules.
253+
249254## ` import.meta `
250255
251256* {Object}
@@ -276,6 +281,9 @@ const buffer = readFileSync(new URL('./data.proto', import.meta.url));
276281 is specified, the value of ` import.meta.url` is used as the default.
277282* Returns: {Promise}
278283
284+ Provides a module-relative resolution function scoped to each module, returning
285+ the URL string.
286+
279287<!-- eslint-skip -->
280288` ` ` js
281289const dependencyAsset = await import .meta.resolve(' component-lib/asset.css' );
@@ -290,34 +298,29 @@ await import.meta.resolve('./dep', import.meta.url);
290298` ` `
291299
292300This function is asynchronous because the ES module resolver in Node.js is
293- asynchronous.
294-
295- ## ` import ()` expressions
296-
297- [Dynamic ` import ()` ][] is supported in both CommonJS and ES modules. In CommonJS
298- modules it can be used to load ES modules.
301+ allowed to be asynchronous.
299302
300303## Interoperability with CommonJS
301304
302- ### ` require`
303-
304- ` require` always treats the files it references as CommonJS.
305-
306- Using ` require` to load an ES module is not supported because ES modules have
307- asynchronous execution. Instead, use use [` import ()` ][] to load an ES module
308- from a CommonJS module.
309-
310305### ` import ` statements
311306
312307An ` import` statement can reference an ES module or a CommonJS module.
313- ` import` statements are permitted only in ES modules. For similar functionality
314- in CommonJS, see [ ` import() ` ][] .
308+ ` import` statements are permitted only in ES modules, but dynamic [ ` import() ` ][]
309+ expressions are supported in CommonJS for loading ES modules .
315310
316311When importing [CommonJS modules](#esm_commonjs_namespaces), the
317312` module.exports` object is provided as the default export. Named exports may be
318313available, provided by static analysis as a convenience for better ecosystem
319314compatibility.
320315
316+ ### ` require`
317+
318+ The CommonJS module ` require` always treats the files it references as CommonJS.
319+
320+ Using ` require` to load an ES module is not supported because ES modules have
321+ asynchronous execution. Instead, use use [` import()` ][] to load an ES module
322+ from a CommonJS module.
323+
321324### CommonJS Namespaces
322325
323326CommonJS modules consist of a ` module.exports` object which can be of any type.
@@ -401,52 +404,67 @@ Named exports detection covers many common export patterns, reexport patterns
401404and build tool and transpiler outputs. See [cjs-module-lexer][] for the exact
402405semantics implemented.
403406
404- ### CommonJS, JSON, and native modules
407+ ### Differences between ES modules and CommonJS
408+
409+ #### No ` require` , ` exports ` or ` module .exports `
410+
411+ In most cases, the ES module ` import ` can be used to load CommonJS modules.
405412
406- CommonJS, JSON, and native modules can be used with
413+ If needed, a ` require ` function can be constructed within an ES module using
407414[` module.createRequire()` ][].
408415
409- ` ` ` js
410- // cjs.cjs
411- module .exports = ' cjs' ;
416+ #### No ` __filename` or ` __dirname`
412417
413- // esm.mjs
414- import { createRequire } from ' module' ;
418+ These CommonJS variables are not available in ES modules.
415419
416- const require = createRequire (import .meta.url);
420+ ` __filename` and ` __dirname` use cases can be replicated via
421+ [` import.meta.url` ][].
417422
418- const cjs = require (' ./cjs.cjs' );
419- cjs === ' cjs' ; // true
420- ` ` `
423+ #### No JSON Module Loading
421424
422- ### Differences between ES modules and CommonJS
425+ JSON imports are still experimental and only supported via the
426+ ` --experimental-json-modules` flag.
423427
424- #### No ` NODE_PATH `
428+ Local JSON files can be loaded relative to ` import.meta.url ` with ` fs ` directly:
425429
426- ` NODE_PATH ` is not part of resolving ` import ` specifiers. Please use symlinks
427- if this behavior is desired.
430+ <!-- eslint-skip -->
431+ ` ` ` js
432+ import { promises as fs } from ' fs' ;
428433
429- #### No ` require` , ` exports` , ` module.exports` , ` __filename` , ` __dirname`
434+ const json = JSON .parse (await fs .readFile (' ./data.json' , import .meta.url));
435+ ` ` `
430436
431- These CommonJS variables are not available in ES modules .
437+ Alterantively ` module . createRequire () ` can be used .
432438
433- ` require ` can be imported into an ES module using [ ` module.createRequire() ` ][].
439+ #### No Native Module Loading
434440
435- Equivalents of ` __filename` and ` __dirname` can be created inside of each file
436- via [` import.meta.url` ][].
441+ Native modules are not currently supported with ES module imports.
442+
443+ They can be loaded directly with ` process .dlopen ` :
437444
438445` ` ` js
446+ import process from ' process' ;
439447import { fileURLToPath } from ' url' ;
440- import { dirname } from ' path' ;
441448
442- const __filename = fileURLToPath ( import .meta.url) ;
443- const __dirname = dirname ( __filename );
449+ const module = { exports : {} } ;
450+ process . dlopen ( module , fileURLToPath ( new URL ( ' ./local.node ' , import .meta.url)) );
444451` ` `
445452
453+ Alternatively ` module .createRequire ()` can be used.
454+
446455#### No ` require .resolve `
447456
448- Former use cases relying on ` require .resolve ` to determine the resolved path
449- of a module can be supported via [` import .meta.resolve` ][].
457+ Relative resolution can be handled via ` new URL (' ./local' , import .meta.url)` .
458+
459+ For a complete ` require.resolve` replacement, there is a flagged experimental
460+ [` import.meta.resolve` ][] API.
461+
462+ Alternatively ` module.createRequire()` can be used.
463+
464+ #### No ` NODE_PATH`
465+
466+ ` NODE_PATH` is not part of resolving ` import` specifiers. Please use symlinks
467+ if this behavior is desired.
450468
451469#### No ` require.extensions`
452470
@@ -455,7 +473,8 @@ hooks can provide this workflow in the future.
455473
456474#### No ` require.cache`
457475
458- ` require.cache` is not used by ` import` . It has a separate cache.
476+ ` require.cache` is not used by ` import` as the ES module loader has its own
477+ separate cache.
459478
460479<i id="esm_experimental_json_modules"></i>
461480
0 commit comments