Skip to content

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

Closed

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 ✔

Activity

  1. dnalborczyk commented on May 11, 2022

    @dnalborczyk
    Contributor

    @nujarum is this issue about import not working at all, or not working in v16?

    depending on which, you did not (re-)export anything, if you wanted to see the module content in the console.

    const scriptText = `
      export * from 'path'
      export const foo = 'bar'
    `
    
    const { sep } = await import(`data:text/javascript,${scriptText}`)
    console.log(sep)
  2. nujarum commented on May 12, 2022

    @nujarum
    Author

    As far as I can tell, this issue only occurs in v16.15 (not v16.14).

    I have added the table of versions I have checked.

    Version OK or Not
    v14.19.2 ✔
    v16.14.2 ✔
    v16.15.0 ❌
    v18.1.0 ✔

    Note that changing path to node:path made no difference in behavior.

  3. nujarum commented on May 12, 2022

    @nujarum
    Author

    This may be a similar issue to #42890.

  4. dnalborczyk commented on May 12, 2022

    @dnalborczyk
    Contributor

    since you corrected the original issue now it wasn't clear to what you were referring to.

  5. changed the title [-]cannot import in `data:` URL script[/-] [+]cannot import built-ins in `data:` URL module[/+] on May 12, 2022
  6. aduh95 commented on May 12, 2022

    @aduh95
    Contributor

    Not exactly related to your problem, but you should always encode the string you pass to be URL-friendly and avoid surprises, e.g. by passing encodeURIComponent(scriptText)) instead of scriptText.

    const scriptText = `
    import path from 'path';
    export const foo = 'bar';
    `.trim();
    
    await import(`data:text/javascript,${scriptText}`); // Wrong, only works by accident if the JS contains no URL special chars.
    await import(`data:text/javascript,${encodeURIComponent(scriptText)}`); // Better, works all the times!

    This issue is a duplicate of #42860, it has been fixed #42881, it should be backported to v16.x in the next release.

  7. nujarum commented on May 12, 2022

    @nujarum
    Author

    @aduh95 Thanks for your reply. And sorry for the duplicate registration.

  8. ChocolateLoverRaj commented on Jul 27, 2022

    @ChocolateLoverRaj

    This issue is a duplicate of #42860, it has been fixed #42881, it should be backported to v16.x in the next release.

    @aduh95 I tried the same example with v16.16.0 and it still has the same error. Is it supposed to be fixed in v16.16.0 or some version >16.16.0?

  9. aduh95 commented on Jul 28, 2022

    @aduh95
    Contributor

    v16.16.0 doesn't have the fix (it was a security release, so it only includes the security fixes), so that's expected. It's supposed to be fixed in the next v16.x release though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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