You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bun build bundling of CJS require("node:assert") does not properly expose exports. and returns an empty {} object #19808
and repeat step 2 ( build ) and step 3 (run) again
we also get an get another error
2101|*/2102|});2103|2104|// lib2.ts2105|varAssert=require_assert();2106|Assert(true,"This should be true");^TypeError: Assertisnotafunction.(In'Assert(!0, "This should be true")','Assert'isaninstanceofObject)
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
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.
building with --target=node runs and compiles to this
import{createRequire}from"node:module";var__require=/* @__PURE__ */createRequire(import.meta.url);// index.tsvarAssert=__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:assertvarrequire_assert=__commonJS(()=>{vardi=Object.create;varRe=Object.defineProperty;
...
varAssert=require_assert();Assert(true,"This should be true");Assert.equal(1,1);console.log("Success!");
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
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
Activity
RiskyMH commentedon May 21, 2025
I think this should work if you add
--target=nodeor--target=bunas the default browser doesn't have assert so its doing who knows what trying to imitate/pollyfill it.mizulu commentedon May 21, 2025
@RiskyMH
building with
--target=noderuns and compiles to thisThe problem is indeed with the browser build. and probably related to how it handles the
__commonJS import
--target=browserhere is some of the compiled code
the Assert ends up as a
{}I thinkmizulu commentedon May 21, 2025
The issue might actually be related to the code snippet above
If this is the signature of the
__commonJSfunctionwe can see that the function being passed to
__commonJSdoes not even takes any argumentswhich can explain why
require_assert()just return a{}[-]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[/+][-]`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[/+]Jarred-Sumner commentedon Sep 2, 2025
Confirmed this continues to reproduce in Bun v1.2.21
test: require both node:assert and assert, and node:events and events
Add the #29985 and #19808 programs to the require(esm) tests