Skip to content

Invalid URLs in static imports do not produce a helpful error message #49571

@renhiyama

Description

@renhiyama

Version

v18.17.1

Platform

Linux desktop 6.1.51 #1-NixOS SMP PREEMPT_DYNAMIC Sat Sep 2 07:16:20 UTC 2023 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

test.js file:

import "file://hmm.js";

Run this test.js file. DO NOT CREATE hmm.js file.

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

Always. The required condition for the importing file to not exist.

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

$ node test.js
node:internal/errors:496
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_FILE_URL_HOST]: File URL host must be "localhost" or empty on linux
    // error stack should point to my file, here in this case, "test.js:1:7" or something similar.
   // it would be helpful in debugging and find out where the problem is in my code...
}

What do you see instead?

$ node test.js
node:internal/errors:496
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_FILE_URL_HOST]: File URL host must be "localhost" or empty on linux
    at new NodeError (node:internal/errors:405:5)
    at getPathFromURLPosix (node:internal/url:1381:11)
    at fileURLToPath (node:internal/url:1404:50)
    at finalizeResolution (node:internal/modules/esm/resolve:294:14)
    at moduleResolve (node:internal/modules/esm/resolve:943:10)
    at defaultResolve (node:internal/modules/esm/resolve:1129:11)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:835:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40) {
  code: 'ERR_INVALID_FILE_URL_HOST'
}

As seen above, nodejs logs internals, instead of linking to my source code. Makes it harder for debug.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    errorsIssues and PRs related to JavaScript errors originated in Node.js core.esmIssues and PRs related to the ECMAScript Modules implementation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions