Skip to content

bun build bundling of CJS require("node:assert") does not properly expose exports. and returns an empty {} object #19808

Description

@mizulu

What version of Bun is running?

1.2.13

What platform is your computer?

x64 win

What steps can reproduce the bug?

step 1 ( create file)

  1. create index.ts file
const Assert = require('node:assert');
Assert.equal(1,1)
Assert(true, "This should be true")
console.log("Success!");

run

> bun index.ts

we should see "Success!" logged

step 2 (build)

  1. build the file
    > bun build index.ts --outdir dist

step 3 (run)

  1. run the build
    > bun ./dist/index.js

step 4

  1. get this error instead of "Success!"
2101 |    */
2102 | });
2103 | 
2104 | // lib2.ts
2105 | var Assert = require_assert();
2106 | Assert.equal(1, 1);
              ^
TypeError: Assert.equal is not a function. (In 'Assert.equal(1, 1)', 'Assert.equal' is undefined)

step 5 ( 2nd case )

  1. comment out the line from index.ts
 // Assert.equal(1,1)

and repeat step 2 ( build ) and step 3 (run) again
we also get an get another error

2101 |    */
2102 | });
2103 | 
2104 | // lib2.ts
2105 | var Assert = require_assert();
2106 | Assert(true, "This should be true");
       ^
TypeError: Assert is not a function. (In 'Assert(!0, "This should be true")', 'Assert' is an instance of Object)

What is the expected behavior?

running bun index.ts
and running the build of index.ts
should both print Success!

What do you see instead?

errors when asserting

Assert.equal(1,1)
or
Assert(true, "This should be true")

Additional information

minimal reproduction extracted, original use case was using @babel/core

discord

Activity

  1. added
    bundlerSomething to do with the bundler
    and removed on May 21, 2025
  2. RiskyMH commented on May 21, 2025

    @RiskyMH
    Contributor

    I think this should work if you add --target=node or --target=bun as the default browser doesn't have assert so its doing who knows what trying to imitate/pollyfill it.

  3. mizulu commented on May 21, 2025

    @mizulu
    ContributorAuthor

    @RiskyMH

    building with --target=node runs and compiles to this

    import { createRequire } from "node:module";
    var __require = /* @__PURE__ */ createRequire(import.meta.url);
    
    // index.ts
    var Assert = __require("node:assert");
    Assert(true, "This should be true");
    Assert.equal(1, 1);
    console.log("Success!");

    The problem is indeed with the browser build. and probably related to how it handles the
    __commonJS import

    --target=browser

    here is some of the compiled code

    var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
    
    // node:assert
    var require_assert = __commonJS(() => {
      var di = Object.create;
      var Re = Object.defineProperty;
     ...
    var Assert = require_assert();
    Assert(true, "This should be true");
    Assert.equal(1, 1);
    console.log("Success!");

    the Assert ends up as a {} I think

  4. mizulu commented on May 21, 2025

    @mizulu
    ContributorAuthor

    The issue might actually be related to the code snippet above

    If this is the signature of the __commonJS function

    var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);

    we can see that the function being passed to __commonJS does not even takes any arguments
    which can explain why require_assert() just return a {}

    // node:assert
    var require_assert = __commonJS(() => {
  5. changed the title [-]usage of require("node:assert") works when running ts file, but throws after running the bundle `bun build`.[/-] [+]`bun build` bundling of CJS `require("node:assert")` does not properly expose exports. and returns an empty `{}` object[/+] on May 21, 2025
  6. changed the title [-]`bun build` bundling of CJS `require("node:assert")` does not properly expose exports. and returns an empty `{}` object[/-] [+]`bun build` bundling of CJS `require("node:assert")` does not properly expose exports. and returns an empty `{}` object[/+] on Jul 17, 2025
  7. Jarred-Sumner commented on Sep 2, 2025

    @Jarred-Sumner
    Collaborator

    Confirmed this continues to reproduce in Bun v1.2.21

  8. added 2 commits that reference this issue on Sep 4, 2026
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

    bugSomething isn't workingbundlerSomething to do with the bundlerconfirmed bugWe can reproduce this issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions