Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
philSixZero opened this issue Dec 2, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@philSixZero
Copy link

philSixZero commented Dec 2, 2024

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] }
@Brooooooklyn Brooooooklyn self-assigned this Dec 3, 2024
@Brooooooklyn Brooooooklyn added the bug Something isn't working label Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants