An imported .cjs file is refused as CommonJS, wherever its specifier points
Date: 2026-11-30
Area: modules, resolution, engine
Issue: #2414
Related: ADR 0212,
ADR 0108,
Module Resolution
Context
.cjs names CommonJS: Node.js loads a .cjs file as CommonJS whatever its package.json says. GocciaScript has
no CommonJS loader. The node_modules resolver refused a .cjs file inside a package with
Package "<name>" resolved to a CommonJS file (<path>); GocciaScript loads only ES modules, and
ADR 0212 made a .cjs entry always a Script. But a relative or
absolute import of a .cjs file, import { v } from "./legacy.cjs", loaded it as an ES module: ADR 0212 recorded
that such an import "still loads it as a module, as every imported file is module source". So export worked in
the file, and require, module and exports did not exist in it.
The maintainer decided #2414 on 2026-11-30, for its first option: refuse every imported .cjs file, relative or
absolute specifier, with the package resolver's CommonJS error, wherever it comes from.
Decision
- No import loads a
.cjsfile as module code. A staticimport, a re-export and a dynamicimport()of one fail to resolve, before the file is read, whatever the file holds. That covers a relative or absolute specifier, an--alias, an import-map entry, a provider package's own import, and sandbox mode. The failure isEModuleIsCommonJS, the package resolver's exception, raised from resolution: it is catchable fromimport(), never aSyntaxError, and the file never runs. - The file decides, not the name it is reached by. The check reads the real path the loader reads, so a
.jssymlink to a.cjsfile is refused and a.cjssymlink to an ES module loads, as Node.js judges the real path. - A typed import still loads one.
with { type: "text" },"bytes"or a data type reads the file without running it, whatever its extension (ADR 0072), so the refusal applies only to an import without a type. - The message names the specifier. Outside a package there is no package name, so the message is
Module "./legacy.cjs" is a CommonJS file; GocciaScript loads only ES modules, with the specifier as written (ADR 0108). Host reporters print the resolved path on theResolved to:line, as for the package refusal. A package's.cjsfile keeps its package-named message. - One place decides it in the engine. The engine's resolution hook (
HostResolveSpecinGoccia.HostAPI), which every host's imports go through, refuses a resolved.cjsfile after the read policy has judged it.ResolvePathinGoccia.Modulesrefuses it the same way when no host resolver is installed. Both take the rule fromIsCommonJSFileNameinGoccia.FileExtensions, as thenode_modulesresolver and ADR 0212's entry goal do, and the message fromCommonJSFileMessageinGoccia.ModuleResolver. A request without a referrer is an entry, not an import, so a.cjsentry still runs as a Script under ADR 0212.
This amends ADR 0212 where it says that a relative or absolute import of a .cjs file still loads it as a module.
Rejected: keeping the load as a module and documenting that only entries and package files are judged by extension. That keeps one file meaning CommonJS in one place and an ES module in another.
Consequences
- A
.cjsfile that holds ES module syntax and was imported by path now fails to resolve. Renaming it to.mjs, or to.js, loads it as a module again. - So does a plain script
.cjsfile imported only for its side effects (import "./setup.cjs"), which ran before. It loads, as module code, once renamed to.jsor.mjs. GocciaBundlercompiles one file and leaves its imports to the run, so bundling an entry that imports a.cjsfile succeeds and running the bytecode fails with the same refusal.tests/language/modules/cjs-import-refusals.jschecks the dynamic forms and typed imports, andscripts/test-cli.tsthe static ones inGocciaRunner,GocciaTestRunner,GocciaScriptLoaderBare, compiled bytecode and sandbox mode, and both symlink directions.