nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨

Home Page:https://nodejs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ESM mistaken for CJS despite --experimental-detect-module

bpstrngr opened this issue · comments

Version

v21.1.0

Platform

x86_64 GNU/Linux

Subsystem

loader

What steps will reproduce the bug?

create a file with a simple export:

// exports.js
let exports = {}, module = {exports};
var index = module.exports;
export { index as default };

and making sure there's no related package.json file with {type:"module"} entry present,
load the file with node --experimental-detect-module ./exports.js .
(i am generating ESM bundles from CJS sources, which end up with such structure)

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

every time

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

such a file should pass as ESM (especially given it's a straightforward target of CJS transpilation).

What do you see instead?

the loader process with --experimental-detect-module flag doesn't detect the ESM file

let exports = {}, module = {
    ^

SyntaxError: Identifier 'exports' has already been declared
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1288:20)
    at Module._compile (node:internal/modules/cjs/loader:1340:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49

Additional information

No response

@nodejs/loaders

I cannot reproduce on main (the process exits with code 0 without printing anything). You indicate version 21.1.0, but surely that’s not what you meant, the flag did not exist on that version.

@aduh95 it is v21.1.0 that it was introduced in: https://nodejs.org/en/blog/release/v21.1.0
to me it looks like this:
image

Oh indeed, my bad, I misread the documentation! Can you try updating to the latest version? v21.x is on maintenance mode and won't receive bug fixes anymore.

sure will do, for the moment substituting the --experimental-default-type=module flag solved my problem, but the overlap and implications of that one are more fuzzy to me than the idea behind detect-module, so will check soon.

The example file is detected successfully in 22.2.0.

oof ok thanks!