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

Unclear error message when importing a module that does not exist from a data URL module #51444

Open
uhyo opened this issue Jan 12, 2024 · 1 comment
Labels
loaders Issues and PRs related to ES module loaders

Comments

@uhyo
Copy link

uhyo commented Jan 12, 2024

Version

21.5.0

Platform

Linux Lenovo-X13 5.15.133.1-microsoft-standard-WSL2 #1 SMP Thu Oct 5 21:02:42 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Run this code:

// test.mjs
await import(`data:text/javascript,${
  encodeURIComponent(`
import "this-does-not-exist";
  `)
}`)

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

always

What is the expected behavior? Why is that the expected behavior?

Since this-does-not-exist does not exist, Node.js should throw a ERR_MODULE_NOT_FOUND error.

Elsewise, Node.js could print a descriptive message about the limitations of data URL modules that they can't import from node_modules or from relative paths.

What do you see instead?

Instead, an ERR_INVALID_URL error is thrown. The error mentions ./package.json, making it feel like an internal error.

node:internal/url:775
    this.#updateContext(bindingUrl.parse(input, base));
                                   ^

TypeError: Invalid URL
    at new URL (node:internal/url:775:36)
    at getPackageScopeConfig (node:internal/modules/esm/package_config:29:24)
    at packageResolve (node:internal/modules/esm/resolve:798:25)
    at moduleResolve (node:internal/modules/esm/resolve:901:20)
    at defaultResolve (node:internal/modules/esm/resolve:1121:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
    at link (node:internal/modules/esm/module_job:84:36) {
  code: 'ERR_INVALID_URL',
  input: './package.json',
  base: 'data:text/javascript,%0Aimport%20%22this-does-not-exist%22%3B%0A%20%20'
}

Additional information

Relevant document says that bare specifiers only work for built-in modules. It is normal that the import fails. So this issue is only about the error message.

@aduh95
Copy link
Contributor

aduh95 commented Jan 12, 2024

FWIW here's the error on other major runtimes:

  • Chromium: TypeError: Failed to resolve module specifier "this-does-not-exist". Relative references must start with either "/", "./", or "../".
  • Firefox: TypeError: The specifier “this-does-not-exist” was a bare specifier, but was not remapped to anything. Relative module specifiers must start with “./”, “../” or “/”.
  • Safari: TypeError: Module name, 'this-does-not-exist' does not resolve to a valid URL.
  • Deno: TypeError: Relative import path "this-does-not-exist" not prefixed with / or ./ or ../

/cc @nodejs/loaders

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
loaders Issues and PRs related to ES module loaders
Projects
None yet
Development

No branches or pull requests

3 participants