Skip to content

TypeError: Expected CommonJS module to have a function wrapper. If you weren't messing around with Bun's internals, this is a bug in Bun #25398

@hamidrezahanafi

Description

@hamidrezahanafi

What version of Bun is running?

1.3.4

What platform is your computer?

Darwin 24.6.0 arm64 arm

What steps can reproduce the bug?

I originally found this issue with our NextJS Webpack build and bun would not be able to run it and I was able to create a very easy way to reproduce that passes with node and not in bun

cat > /tmp/bun-bug.js << 'REPRO'
// Minimal reproduction for Bun CommonJS loader bug
// Bug: "Expected CommonJS module to have a function wrapper"
// 
// Works: node bun-bug.js
// Fails: bun run --bun bun-bug.js
//
// Root cause: In a Webpack-style CommonJS chunk with arrow function
// using BLOCK BODY (not expression body), the combination of:
// 1. Spread operator (...k)
// 2. Optional chaining + nullish coalescing to object literal (k?.x??{})
// inside a parenthesized expression causes Bun's CJS loader to fail.
//
// The expression body version =>(...) works fine.
// The block body version =>{...} fails.

exports.id=1,exports.ids=[1],exports.modules={1:(a,b,c)=>{let k={};({...k,a:k?.x??{}})}};
REPRO

echo "=== Minimal reproduction ==="
cat /tmp/bun-bug.js
echo ""
echo ""
echo "=== Testing ==="
echo "Node.js:"
node /tmp/bun-bug.js && echo "  SUCCESS" || echo "  FAILED"
echo ""
echo "Bun:"
bun run --bun /tmp/bun-bug.js 2>&1

Result:

=== Testing ===
Node.js:
  SUCCESS

Bun:
 6 |     "use strict";
 7 | 
 8 |     //
 9 |     if (entry.instantiate)
10 |         return entry.instantiate;
11 |     var instantiatePromise = (async () => {
                                               ^
TypeError: Expected CommonJS module to have a function wrapper. If you weren't messing around with Bun's internals, this is a bug in Bun

What is the expected behavior?

Bun should be able to run this like NodeJS and result should be both success

What do you see instead?

TypeError: Expected CommonJS module to have a function wrapper. If you weren't messing around with Bun's internals, this is a bug in Bun

Additional information

No response

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