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

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

Closed
nujarum opened this issue May 11, 2022 · 8 comments
Closed

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

nujarum opened this issue May 11, 2022 · 8 comments

Comments

@nujarum
Copy link

nujarum commented May 11, 2022

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
@dnalborczyk
Copy link
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)

@nujarum
Copy link
Author

nujarum commented May 12, 2022

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.

@nujarum
Copy link
Author

nujarum commented May 12, 2022

This may be a similar issue to #42890.

@dnalborczyk
Copy link
Contributor

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

@aduh95 aduh95 changed the title cannot import in data: URL script cannot import built-ins in data: URL module May 12, 2022
@aduh95
Copy link
Contributor

aduh95 commented May 12, 2022

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.

@aduh95 aduh95 closed this as completed May 12, 2022
@nujarum
Copy link
Author

nujarum commented May 12, 2022

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

@ChocolateLoverRaj
Copy link

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?

@aduh95
Copy link
Contributor

aduh95 commented Jul 28, 2022

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants