Browse documentation

A .cjs entry is always a Script, and an import or export declaration in it is a SyntaxError

Date: 2026-11-29 Area: modules, source pipeline, engine, CLI Issue: #1690 (item 3) Related: ADR 0140, Module Resolution, Language § Entry source type

Context

ADR 0140 detects an entry's goal from its syntax when neither a source type setting nor a .mjs or .mts file name has selected it. Its table names no other extension, so a .cjs entry was detected like a .js one: x.cjs holding export {}; ran as a Module, with top-level this undefined. Everywhere else .cjs is CommonJS: the node_modules resolver classifies a .cjs file as CommonJS whatever it contains and refuses to load it (Module Resolution). Node.js treats a .cjs file as CommonJS whatever its package.json says, and an import or export declaration in one is a SyntaxError.

An explicit Script does not refuse them either. ADR 0140 kept GocciaScript's acceptance of import and export declarations in an explicit Script (--source-type=script): the imports run as a module of their own, and each exported declaration declares its binding as it would without export. ECMA-262 has no ImportDeclaration or ExportDeclaration in a ScriptBody (ES2026 §16.1 Scripts, §16.2.2 Imports, §16.2.3 Exports), so making a .cjs entry a Script alone would not make its module syntax an error.

The maintainer decided #1690's item 3 on 2026-11-29: "Always a Script".

Decision

  • A .cjs entry is always a Script. Its extension decides its goal whatever else names one: the engine's SourceKind, --source-type, and a root or per-file "source-type". This is where .cjs differs from .mjs, whose Module goal an explicit setting overrides. A config that makes a directory's entries Modules does not turn a .cjs file in it into one.
  • An import or export declaration in it is a SyntaxError. A .cjs entry keeps none of an explicit Script's acceptance of module syntax. Its first top-level import declaration, export declaration or re-export fails the compile, before anything runs, at that declaration's line and column: A .cjs file is always a Script, and an export declaration belongs to a module (or an import declaration). The rest follows from the Script goal: a dynamic import() works, import.meta is a SyntaxError, and so is top-level await, since await is an identifier in a Script outside async functions.
  • One place decides it. EntryGoal in Goccia.Embed maps an entry's file name and SourceKind to the goal HostCompileEntry (Goccia.Eval) compiles under, for Run, CompileArtifact and a heap snapshot's restore. Goal 3 is a Script whose module declarations RefuseModuleDeclarations turns into the SyntaxError. So every host that runs a file through the engine gets the rule: GocciaRunner (host and sandbox mode), GocciaTestRunner, GocciaBenchmarkRunner, GocciaBundler, GocciaScriptLoaderBare and embedders. A .gbc file compiled from a .cjs entry is a Script file and runs as one. GocciaScriptLoaderBare --source-name names stdin only, as its help says, so it cannot give a named .cjs file another extension.
  • The parse finds the declaration. TGocciaParser notes the first top-level statement that starts as an import or export declaration before it parses it, so a declaration the parse erases (a type-only one, or one --warning-unsupported-features skips) is refused too, at its line and column in the source as written.
  • A .cjs entry is a Script, not a CommonJS module. GocciaScript has no CommonJS loader: there is no require, module or exports binding. This decision is about entries: the node_modules resolver still refuses a .cjs file inside a package, and a relative or absolute import of a .cjs file still loads it as a module, as every imported file is module source.

Rejected: a .cjs entry as an ordinary explicit Script, which would accept its import and export declarations as ADR 0140 lets --source-type=script do. That keeps code Node.js rejects running, and it is not the decision.

Consequences

  • A .cjs entry with module syntax that used to run as a detected Module now fails with a SyntaxError. Renaming it to .mjs, or to .js for detection, runs it as a Module again.
  • Directory runs are unchanged: a .cjs file is not a script extension (ScriptExtensions in Goccia.FileExtensions), so only a .cjs file named on the command line is an entry.
  • scripts/test-cli.ts checks the rule in each host, against --source-type=module, --source-type=script and a config's "source-type": "module".

Refined by ADR 0223: a .cjs file imported as module code is refused as CommonJS, so a relative or absolute import no longer loads one as a module.