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
.cjsentry is always a Script. Its extension decides its goal whatever else names one: the engine'sSourceKind,--source-type, and a root or per-file"source-type". This is where.cjsdiffers from.mjs, whose Module goal an explicit setting overrides. A config that makes a directory's entries Modules does not turn a.cjsfile in it into one. - An import or export declaration in it is a
SyntaxError. A.cjsentry 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(oran import declaration). The rest follows from the Script goal: a dynamicimport()works,import.metais aSyntaxError, and so is top-levelawait, sinceawaitis an identifier in a Script outside async functions. - One place decides it.
EntryGoalinGoccia.Embedmaps an entry's file name andSourceKindto the goalHostCompileEntry(Goccia.Eval) compiles under, forRun,CompileArtifactand a heap snapshot's restore. Goal 3 is a Script whose module declarationsRefuseModuleDeclarationsturns into theSyntaxError. So every host that runs a file through the engine gets the rule:GocciaRunner(host and sandbox mode),GocciaTestRunner,GocciaBenchmarkRunner,GocciaBundler,GocciaScriptLoaderBareand embedders. A.gbcfile compiled from a.cjsentry is a Script file and runs as one.GocciaScriptLoaderBare --source-namenames stdin only, as its help says, so it cannot give a named.cjsfile another extension. - The parse finds the declaration.
TGocciaParsernotes 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-featuresskips) is refused too, at its line and column in the source as written. - A
.cjsentry is a Script, not a CommonJS module. GocciaScript has no CommonJS loader: there is norequire,moduleorexportsbinding. This decision is about entries: thenode_modulesresolver still refuses a.cjsfile inside a package, and a relative or absolute import of a.cjsfile 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
.cjsentry with module syntax that used to run as a detected Module now fails with aSyntaxError. Renaming it to.mjs, or to.jsfor detection, runs it as a Module again. - Directory runs are unchanged: a
.cjsfile is not a script extension (ScriptExtensionsinGoccia.FileExtensions), so only a.cjsfile named on the command line is an entry. scripts/test-cli.tschecks the rule in each host, against--source-type=module,--source-type=scriptand 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.