node/test/es-module/test-esm-cjs-load-error-note.mjs
Joyee Cheung cad46afc07
module: support require()ing synchronous ESM graphs
This patch adds `require()` support for synchronous ESM graphs under
the flag `--experimental-require-module`

This is based on the the following design aspect of ESM:

- The resolution can be synchronous (up to the host)
- The evaluation of a synchronous graph (without top-level await) is
  also synchronous, and, by the time the module graph is instantiated
  (before evaluation starts), this is is already known.

If `--experimental-require-module` is enabled, and the ECMAScript
module being loaded by `require()` meets the following requirements:

- Explicitly marked as an ES module with a `"type": "module"` field in
  the closest package.json or a `.mjs` extension.
- Fully synchronous (contains no top-level `await`).

`require()` will load the requested module as an ES Module, and return
the module name space object. In this case it is similar to dynamic
`import()` but is run synchronously and returns the name space object
directly.

```mjs
// point.mjs
export function distance(a, b) {
  return (b.x - a.x) ** 2 + (b.y - a.y) ** 2;
}
class Point {
  constructor(x, y) { this.x = x; this.y = y; }
}
export default Point;
```

```cjs
const required = require('./point.mjs');
// [Module: null prototype] {
//   default: [class Point],
//   distance: [Function: distance]
// }
console.log(required);

(async () => {
  const imported = await import('./point.mjs');
  console.log(imported === required);  // true
})();
```

If the module being `require()`'d contains top-level `await`, or the
module graph it `import`s contains top-level `await`,
[`ERR_REQUIRE_ASYNC_MODULE`][] will be thrown. In this case, users
should load the asynchronous module using `import()`.

If `--experimental-print-required-tla` is enabled, instead of throwing
`ERR_REQUIRE_ASYNC_MODULE` before evaluation, Node.js will evaluate the
module, try to locate the top-level awaits, and print their location to
help users fix them.

PR-URL: https://github.com/nodejs/node/pull/51977
Backport-PR-URL: https://github.com/nodejs/node/pull/53500
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2024-08-08 15:02:48 +02:00

97 lines
2.8 KiB
JavaScript

import { spawnPromisified } from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import assert from 'node:assert';
import { execPath } from 'node:process';
import { describe, it } from 'node:test';
// Expect note to be included in the error output
// Don't match the following sentence because it can change as features are
// added.
const expectedNote = 'Warning: To load an ES module';
const mustIncludeMessage = {
getMessage: (stderr) => `${expectedNote} not found in ${stderr}`,
includeNote: true,
};
const mustNotIncludeMessage = {
getMessage: (stderr) => `${expectedNote} must not be included in ${stderr}`,
includeNote: false,
};
describe('ESM: Errors for unexpected exports', { concurrency: true }, () => {
for (
const { errorNeedle, filePath, getMessage, includeNote }
of [
{
// name: '',
filePath: fixtures.path('/es-modules/es-note-unexpected-export-1.cjs'),
...mustIncludeMessage,
},
{
// name: '',
filePath: fixtures.path('/es-modules/es-note-unexpected-import-1.cjs'),
...mustIncludeMessage,
},
{
// name: '',
filePath: fixtures.path('/es-modules/es-note-promiserej-import-2.cjs'),
...mustNotIncludeMessage,
},
{
// name: '',
filePath: fixtures.path('/es-modules/es-note-unexpected-import-3.cjs'),
...mustIncludeMessage,
},
{
// name: '',
filePath: fixtures.path('/es-modules/es-note-unexpected-import-4.cjs'),
...mustIncludeMessage,
},
{
// name: '',
filePath: fixtures.path('/es-modules/es-note-unexpected-import-5.cjs'),
...mustNotIncludeMessage,
},
{
// name: '',
filePath: fixtures.path('/es-modules/es-note-error-1.mjs'),
...mustNotIncludeMessage,
errorNeedle: /Error: some error/,
},
{
// name: '',
filePath: fixtures.path('/es-modules/es-note-error-2.mjs'),
...mustNotIncludeMessage,
errorNeedle: /string/,
},
{
// name: '',
filePath: fixtures.path('/es-modules/es-note-error-3.mjs'),
...mustNotIncludeMessage,
errorNeedle: /null/,
},
{
// name: '',
filePath: fixtures.path('/es-modules/es-note-error-4.mjs'),
...mustNotIncludeMessage,
errorNeedle: /undefined/,
},
]
) {
it(`should ${includeNote ? '' : 'NOT'} include note`, async () => {
const { code, stderr } = await spawnPromisified(execPath, [filePath]);
assert.strictEqual(code, 1);
if (errorNeedle != null) assert.match(stderr, errorNeedle);
const shouldIncludeNote = stderr.includes(expectedNote);
assert.ok(
includeNote ? shouldIncludeNote : !shouldIncludeNote,
`${filePath} ${getMessage(stderr)}`,
);
});
}
});