Skip to content

[@swc-node/register] import of module using file URL dynamically fails #883

@philSixZero

Description

@philSixZero

Description

@swc-node/register fails to import module with file URL since v1.10.0.

Reproduction

Minimal reproduction example with the following files:

├── ...
├── test.ts
├── module.mjs
└── ...

Content of test.ts:

import { join } from 'node:path';
import { cwd } from 'node:process';

async function importModule() {
  const file = './module.mjs';
  const filePath = `file:///${join(cwd(), file)}`;
  console.log('Importing module:', filePath);
  const module = await import(filePath);
  console.log('Imported module:', module);
}

importModule();

Content of module.mjs:

export function hello() {
    return 'Hello, world!';
}

Run the test.ts file, using this command:

node --import @swc-node/register/esm-register test.ts

Result

Importing module: file:///E:\My\Path\module.mjs

node:internal/process/promises:394
    triggerUncaughtException(err, true /* fromPromise */);
    ^
TypeError [ERR_INVALID_RETURN_PROPERTY_VALUE]: Expected undefined or a fully resolved URL string to be returned for the "responseURL" from the "node:internal/modules/esm/load 'load'" hook but got type string ('file:///...').
    at Hooks.load (node:internal/modules/esm/hooks:412:13)
    at async MessagePort.handleMessage (node:internal/modules/esm/worker:199:18) {
  code: 'ERR_INVALID_RETURN_PROPERTY_VALUE'
}

Node.js v22.11.0

With versions older than v1.10.0, the result is expected:

Importing module: file:///E:\My\Path\module.mjs
Imported module: [Module: null prototype] { hello: [Function: hello] }

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions