Skip to content

A byte order mark before a shebang makes the file fail to parse #846

Closed

Description

@fengmk2

A file that starts with a byte order mark followed by a shebang fails to load:

$ printf '\xef\xbb\xbf#!/usr/bin/env node\nconsole.log("bom+shebang");\n' > main.ts

$ node main.ts
bom+shebang
$ node --import @oxc-node/core/register main.ts
Error: Failed to parse file:///…/main.ts: Invalid Character `!`

A BOM alone loads fine, and so does the same file saved as .mjs under plain Node.js. Editors on Windows often save files with a BOM, so a shebang script written there cannot run under oxc-node, while tsx runs it. Reproduced with @oxc-node/core@0.1.7 from npm on macOS arm64 with Node.js 24.12, and on 22.18 and 26.5 through an embedder that compiles in v0.1.7.

Cause

The oxc parser follows the spec: a hashbang is recognized only at the very start of the source, and a BOM is whitespace, so #! after it is an error. Hosts strip the BOM before parsing; Node.js's CommonJS loader does it with stripBOM, its ES module loader by decoding with TextDecoder, and oxc's own linter and formatter strip it before calling the parser. oxc-node passes the source to the parser with the BOM still in place. (Reported first as oxc-project/oxc#27740, which is closed in favor of this issue.)

Suggested fix

Strip a leading U+FEFF from the source before parsing, in both the load hook and the CommonJS transform.

Activity

  1. added a commit that references this issue on Oct 17, 2026
    440c31f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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