Skip to content

cannot import built-ins in data: URL module #43060

@nujarum

Description

@nujarum

Version

v16.15.0

Platform

Microsoft Windows NT 10.0.19044.0 x64

Subsystem

No response

What steps will reproduce the bug?

const scriptText = `
import path from 'path';
export const foo = 'bar';
`.trim();

const module = await import(`data:text/javascript,${scriptText}`);
console.log(module);

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior?

According to the data: Imports section of the documentation, the following is mentioned:

data: URLs only resolve Bare specifiers for builtin modules and Absolute specifiers.

If this documentation is correct, I believe it should be able to import the builtin module path successfully in a data: URL script.

What do you see instead?

$ node index.mjs 
node:internal/errors:465
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file
?[90m    at new NodeError (node:internal/errors:372:5)?[39m
?[90m    at fileURLToPath (node:internal/url:1422:11)?[39m
?[90m    at checkIfDisallowedImport (node:internal/modules/esm/resolve:1061:23)?[39m
?[90m    at defaultResolve (node:internal/modules/esm/resolve:1189:23)?[39m
?[90m    at ESMLoader.resolve (node:internal/modules/esm/loader:580:30)?[39m
?[90m    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:294:18)?[39m
?[90m    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:80:40)?[39m
?[90m    at link (node:internal/modules/esm/module_job:78:36)?[39m {
  code: ?[32m'ERR_INVALID_URL_SCHEME'?[39m
}

Additional information

In another Node.js version, the output is as follows:

$ node index.mjs
[Module: null prototype] { foo: 'bar' }
Version OK or Not
v14.19.2
v16.14.2
v16.15.0
v18.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions