Browse documentation

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 .cjs file as module code. A static import, a re-export and a dynamic import() 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 is EModuleIsCommonJS, the package resolver's exception, raised from resolution: it is catchable from import(), never a SyntaxError, 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 .js symlink to a .cjs file is refused and a .cjs symlink 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 the Resolved to: line, as for the package refusal. A package's .cjs file keeps its package-named message.
  • One place decides it in the engine. The engine's resolution hook (HostResolveSpec in Goccia.HostAPI), which every host's imports go through, refuses a resolved .cjs file after the read policy has judged it. ResolvePath in Goccia.Modules refuses it the same way when no host resolver is installed. Both take the rule from IsCommonJSFileName in Goccia.FileExtensions, as the node_modules resolver and ADR 0212's entry goal do, and the message from CommonJSFileMessage in Goccia.ModuleResolver. A request without a referrer is an entry, not an import, so a .cjs entry 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 .cjs file 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 .cjs file imported only for its side effects (import "./setup.cjs"), which ran before. It loads, as module code, once renamed to .js or .mjs.
  • GocciaBundler compiles one file and leaves its imports to the run, so bundling an entry that imports a .cjs file succeeds and running the bytecode fails with the same refusal.
  • tests/language/modules/cjs-import-refusals.js checks the dynamic forms and typed imports, and scripts/test-cli.ts the static ones in GocciaRunner, GocciaTestRunner, GocciaScriptLoaderBare, compiled bytecode and sandbox mode, and both symlink directions.