oven-sh / bun

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

CommonJS module.exports is not a function #4506

Open yus-ham opened 1 year ago

yus-ham commented 1 year ago

What version of Bun is running?

0.8.2+f73f77d0dec9006f5f42ff401fb32f9eaefa749a

What platform is your computer?

No response

What steps can reproduce the bug?

see https://replit.com/@Yusham1/bun-require-module-exported-as-function#src/index.js

const isNaturalNumber = require('is-natural-number')

console.info(isNaturalNumber)
console.info(isNaturalNumber(123))

What is the expected behavior?

no error

What do you see instead?

Module {
  __esModule: true,
  default: [Function: isNaturalNumber]
}

TypeError: isNaturalNumber is not a function. (In 'isNaturalNumber(123)', 'isNaturalNumber' is an instance of Module)

Additional information

No response

Kleywalker commented 9 months ago

Is this related to issue #7636. Are there any plans to implement this soon?

CoolSpot commented 7 months ago

Still an issue in Bun 1.0.29: image

│ │ │ │ ├─┬ decompress-unzip@3.4.0
│ │ │ │ │ ├── is-zip@1.0.0
│ │ │ │ │ ├── read-all-stream@3.1.0 deduped
│ │ │ │ │ ├── stat-mode@0.2.2
│ │ │ │ │ ├── strip-dirs@1.1.1 deduped
│ │ │ │ │ ├── through2@2.0.5
│ │ │ │ │ ├── vinyl@1.2.0
│ │ │ │ │ └── yauzl@2.10.0

Not sure if it is is-natural-number's problem, but it behaves differently than nodejs and thus breaks compatibility.

karlhorky commented 2 months ago

A similar error also happens with ley:

$ bunx --bun ley up
[ley] Loading configuration

[ley] An error occurred:

      TypeError: require("postgres") is not a function. (In 'require("postgres")({
          onnotice: () => {
          },
          ...config,
          max: 1
        })', 'require("postgres")' is an instance of Module)
        at <anonymous> (/Users/k/p/courses/node_modules/ley/lib/clients/postgres.js:5:2)
        at <anonymous> (/Users/k/p/courses/node_modules/ley/index.js:57:25)
        at processTicksAndRejections (:12:39)
      originalLine: 5
      originalColumn: 31

Reproduction steps:

➜  p mkdir a
➜  p cd a

➜  a bun init -y
Done! A package.json file was saved in the current directory.
 + index.ts
 + .gitignore
 + tsconfig.json (for editor auto-complete)
 + README.md

To get started, run:
  bun run index.ts

➜  a bun add postgres
bun add v1.1.21 (70ca2b76)

installed postgres@3.4.4

1 package installed [686.00ms]

➜  a bun add ley
bun add v1.1.21 (70ca2b76)

installed ley@0.8.1 with binaries:
 - ley

6 packages installed [203.00ms]

➜  a mkdir migrations

➜  a bunx --bun ley up

[ley] An error occurred:

      TypeError: require("postgres") is not a function. (In 'require("postgres")({
          onnotice: () => {
          },
          ...config,
          max: 1
        })', 'require("postgres")' is an instance of Module)
        at <anonymous> (/Users/k/p/a/node_modules/ley/lib/clients/postgres.js:5:2)
        at <anonymous> (/Users/k/p/a/node_modules/ley/index.js:38:25)
        at processTicksAndRejections (:12:39)
      originalLine: 5
      originalColumn: 31