Skip to content

Commit

Permalink
fix(esm api): warn on missing module.register
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed May 11, 2024
1 parent e062973 commit dae9f0d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/esm/api/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export type InitializationOptions = {
export const register = (
options?: Options,
) => {
if (!module.register) {
throw new Error(`This version of Node.js (${process.version}) does not support module.register(). Please upgrade to Node v18.9 or v20.6 and above.`);
}

const { sourceMapsEnabled } = process;
process.setSourceMapsEnabled(true);

Expand Down
5 changes: 1 addition & 4 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import { nodeVersions } from './utils/node-versions';
await runTestSuite(import('./specs/cli'), node);
await runTestSuite(import('./specs/watch'), node);
await runTestSuite(import('./specs/loaders'), node);
await runTestSuite(
import('./specs/smoke'),
node,
);
await runTestSuite(import('./specs/smoke'), node);
});
}
});
Expand Down
26 changes: 26 additions & 0 deletions tests/specs/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,32 @@ export default testSuite(({ describe }, node: NodeApis) => {
expect(stdout).toBe('file.ts\nfoo.ts\nfoo');
});
});
} else {
test('no module.register error', async () => {
await using fixture = await createFixture({
'package.json': JSON.stringify({ type: 'module' }),
'register.mjs': `
import { register } from ${JSON.stringify(tsxEsmApiPath)};
{
const unregister = register();
const { message } = await import('./file?2');
console.log(message);
await unregister();
}
`,
...tsFiles,
});

const { stderr } = await execaNode(fixture.getPath('register.mjs'), [], {
nodePath: node.path,
nodeOptions: [],
reject: false,
});
expect(stderr).toMatch(`This version of Node.js (v${node.version}) does not support module.register(). Please upgrade to Node v18.9 or v20.6 and above.`);
});
}
});
});
Expand Down

0 comments on commit dae9f0d

Please sign in to comment.