denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
96.91k stars 5.35k forks source link

"module is not defined" cjs suggestion should not occur for mjs/mts modules #26557

Open dsherret opened 1 hour ago

dsherret commented 1 hour ago
> cat main.mjs
import * as a from "./a.cjs";

console.log(a.add(1, 2));

module.isPreloading = true;
> deno run main.mjs
3                                                                                                                                                                                                                                                                                                                
error: Uncaught (in promise) ReferenceError: module is not defined                                                                                                                                                                                                                                                             
module.isPreloading = true;                                                                                                                                                                                                                                                                                                    
^
    at file:///V:/scratch/main.mjs:5:1

    info: Deno supports CommonJS modules in .cjs files, or when there's a package.json
          with "type": "commonjs" option and --unstable-detect-cjs flag is used.
    hint: Rewrite this module to ESM,
          or change the file extension to .cjs,
          or add package.json next to the file with "type": "commonjs" option
          and pass --unstable-detect-cjs flag.
    docs: https://docs.deno.com/go/commonjs

This suggestion doesn't make sense.

Node for example:

> node main.mjs
3
file:///V:/scratch/main.mjs:5
module.isPreloading = true;
^

ReferenceError: module is not defined in ES module scope
    at file:///V:/scratch/main.mjs:5:1
    at ModuleJob.run (node:internal/modules/esm/module_job:262:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:474:24)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:109:5)

Node.js v22.3.0
dsherret commented 1 hour ago

@bartlomieju is this a good first issue?

Relevant code is here: https://github.com/denoland/deno/blob/d92d2fe9b0bd5e6e29cb3b6f924472aec972b636/runtime/fmt_errors.rs#L307-L309