Skip to content

Building with --format cjs and --no-bundle ignores cjs format #29187

Open

Description

@CosmoCreeper

What version of Bun is running?

1.3.5+1e86cebd7

What platform is your computer?

Microsoft Windows NT 10.0.26200.0 x64

What steps can reproduce the bug?

Create a TypeScript file with some simple imports and exports and build it with a specific build command:
bun build ./index.ts --outfile ./index.js --target node --format cjs --no-bundle

What is the expected behavior?

I'm looking to be able to output a file that is still readable and uses external imports, meanwhile being compatible with CommonJS.

What do you see instead?

An ESM JS file using import and export features.

Additional information

No response

Activity

  1. robobun commented on Apr 11, 2026

    @robobun
    Collaborator

    ✅ Reproduced. Fix in #29190 — --format cjs --no-bundle now routes ESM imports/exports through the CommonJS printer. Covers export function/class, export const/let/var (incl. nested destructuring), export default, export * from, export { ... } from, and ES2022 string-literal export names.

  2. added a commit that references this issue on Apr 11, 2026
    01720b1
  3. robobun commented on Apr 11, 2026

    @robobun
    Collaborator

    Thanks for testing! The previous commits handled every export form but I'd missed the s_import handler entirely — it was still emitting raw import statements regardless of format. Fixed in b7924a1: the --no-bundle --format cjs path now rewrites import to require too (simple destructuring, no runtime helpers). Could you rebuild and retest? The new output for your example should be:

    var { readFileSync } = require("fs");
    var path = require("path");
    function hello() {
      console.log("Hello", readFileSync, path);
    }
    Object.defineProperty(module.exports, "hello", { get: () => hello, enumerable: true, configurable: true });
  4. CosmoCreeper commented on Apr 11, 2026

    @CosmoCreeper
    Author

    Nice job! Can confirm that it's working perfectly now!
    Not sure why you commented here when I replied in the pull request though. Regardless it looks good.

  5. robobun commented on Apr 11, 2026

    @robobun
    Collaborator

    Thanks — that's exactly it. printCommonJS was using a NoOpRenamer unconditionally because historically it was only called from the bundler path (after the linker had already run). On the new no-bundle path the parser runs with minify_identifiers on but never gets a renamer. Fixed in 451c9ba: mirrored printAst"'s MinifyRenamer" setup in printCommonJS", and updated printBundledExportsForBinding" to read symbol.original_name" for the Object.defineProperty" key (external contract) and `renamer.nameForSymbol(ref)" for the getter target (local), so only the local binding shrinks under minify.

  6. robobun commented on Apr 14, 2026

    @robobun
    Collaborator

    Thanks for digging through the log! Yeah that confirms what I suspected — env: bun: No such file or directory / ci-remap server did not start: code 127 is CI agent setup failing to find bun in `PATH" on the darwin-aarch64 runner. Nothing to do with this PR's code: the same pair of darwin-aarch64 lanes failed on 4 out of the 6 most recent merged PRs (#29164, #29153, #29138, #29104 all have it). All 56 other lanes pass.

    Code-side everything looks green. Will ping a maintainer to get this re-triggered or reviewed so it can land. Once it's in a release build, please give it another spin against your original repro!

  7. added a commit that references this issue on May 7, 2026
    b0f97d8
  8. robobun commented on Sep 8, 2026

    @robobun
    Collaborator

    Still reproduces on Bun 1.4.2: bun build ./index.ts --target node --format cjs --no-bundle prints the import/export statements unchanged and exits 0.

    The earlier fix (#29190) was against the Zig sources and never landed. New fix against the current tree: #41988. With it, --no-bundle --format cjs converts each file on its own and keeps every import as a require() of the same specifier:

    var import_fs = require("fs");
    var import_path = __toESM(require("path"));
    function hello() {
      console.log("Hello", import_fs.readFileSync, import_path.default);
    }

    (plus the usual __export/module.exports = __toCommonJS(...) preamble, the same shape esbuild emits for --format=cjs without --bundle).

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions