Skip to content

url: Cannot load ESM module with newline in file name #23696

Closed
@demurgos

Description

@demurgos
  • Version: 10.12.0
  • Platform: Linux 64 bit
  • Subsystem: Modules

Node fails to load ESM entry points if their filename contains a newline character. This is a regression compared to the current commonjs modules.

The following code fails:

fs.writeFileSync("foo\nbar.mjs", "console.log('Hello, World!');\n");
const spawnRes = cp.spawnSync(process.execPath, ["--experimental-modules", "foo\nbar.mjs"]);
assert(spawnRes.status === 0);
console.error(spawnRew.stderr.toString("UTF-8"));

Expected: no failed assertion, nothing in stderr (appart from the warning that ESM is experimental).
Actual: Failed assertion. Stderr contains:

Error: Cannot find module /tmp/tmp-59206v9LQ3sEjvCA/foobar.mjs
    at search (internal/modules/esm/default_resolve.js:28:12)
    at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:64:11)
    at Loader.resolve (internal/modules/esm/loader.js:58:33)
    at Loader.getModuleJob (internal/modules/esm/loader.js:113:40)
    at Loader.import (internal/modules/esm/loader.js:99:28)
    at asyncESM.loaderPromise.then (internal/modules/cjs/loader.js:734:27)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)
    at startup (internal/bootstrap/node.js:279:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:752:3) code: 'MODULE_NOT_FOUND'

Note that the error message does not even contain the newline in the file name.

For comparison, the following code using commonJS works fine:

fs.writeFileSync("foo\nbar.js", "console.log('Hello, World!');\n");
const spawnRes = cp.spawnSync(process.execPath, ["foo\nbar.js"]);
assert(spawnRes.status === 0);
console.error(spawnRew.stderr.toString("UTF-8"));

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.whatwg-urlIssues and PRs related to the WHATWG URL implementation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions