Skip to content

.ts files whose only imports and exports are type-only run as ES modules, without require or module #909

Closed

Description

@darcyrush

While investigating #905 the LLM stated it found another tangential bug. However, one of the impacted tests marked the logic as intentional, so could be a false positive.

Versions: oxc-project/oxc-node main (ffa98c1), @oxc-node/core 1.0.1, Node.js v26.12.1, linux-x64

Repro

package.json (or no "type")

{ "type": "commonjs" }

types.ts

export type Platform = string;

entry.ts

import type { Platform } from "./types";

const os = require("node:os");
const platform: Platform = os.platform();
console.log("ok", typeof platform);
node --import @oxc-node/core/register ./entry.ts

Expected: ok string. Type erasure removes the import type, and nothing else in the file is module syntax, so Node.js' own type stripping (node ./entry.ts) runs it as CommonJS and prints ok string. tsc with module: nodenext emits CommonJS for it too.

Actual:

ReferenceError: require is not defined in ES module scope, you can use import instead

The same happens when the file is require()d from a .cjs file or imported from an .mts file, and with every declaration that type erasure removes whole: export type { … } (with or without from), export type * from, export type X = …, export interface, export default interface and export declare ….

With export =, the file fails earlier:

import type { Platform } from "./types";

const platform: Platform = process.platform;
export = { platform };
Error: Failed to transform …/counter.ts:
  ! TS(1203): Export assignment cannot be used when targeting ECMAScript modules.

When it is require()d, the error is ReferenceError: module is not defined in ES module scope.
tsc compiles it to module.exports = { platform };.

Cause

oxc's parser resolves a file with a type-only import/export to a module, and parsed_as_esm (src/lib.rs) counts those declarations as module syntax, so the load hook reports the file as an ES module. On the require() path, the TypeScript transform appends an empty export {} when it erases the file's last import/export, so the output is an ES module there as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions