oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.08k stars 2.76k forks source link

bundling `require("moment")` throws "moment is not a function" #5004

Open atuttle opened 1 year ago

atuttle commented 1 year ago

What version of Bun is running?

1.0.0+822a00c4d508b54f650933a73ca5f4a3af9a7983

What platform is your computer?

Darwin 22.6.0 arm64 arm

What steps can reproduce the bug?

I created a repro repo here: https://github.com/atuttle/bun-bundler-repro

the short version is:

  1. Install moment using bun install moment
  2. create a JS file that require()'s moment.js and then uses it
  3. bundle that js file using Bun with target=browser (bun run build in my repro repo)
  4. Use something like nws to serve the root of the repo as a website, and open it in your browser. Open the devtools console to see the error that is thrown.

Here's the source of the js file in the project:

const moment = require('moment');

function foo() {
    console.log(moment().add(5, 'minutes'));
}

foo();

and package.json:

{
    "name": "bun-bundle-repro",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "build": "bun build index.js --target browser --outdir ./dist"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
        "moment": "^2.29.4"
    }
}

What is the expected behavior?

a moment object should be logged containing a timestamp 5 minutes in the future.

What do you see instead?

An error is thrown, because the bundle is not valid / well-organized.

image

Additional information

No response

Jarred-Sumner commented 2 months ago

@paperdave does this still reproduce?

paperdave commented 2 months ago

yes