oven-sh / bun

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

TypeError: _module._compile is not a function. (In '_module._compile(source, __filename)', '_module._compile' is undefined) #4590

Closed singh-jay closed 1 year ago

singh-jay commented 1 year ago

What version of Bun is running?

1.0.0+822a00c4d508b54f650933a73ca5f4a3af9a7983

What platform is your computer?

Linux 6.2.0-32-generic x86_64 x86_64

What steps can reproduce the bug?

Create a react app using CRA template(webpack 5)

What is the expected behavior?

it should build as with node it is working fine.

What do you see instead?

bun --bun run build

$ react-scripts build 10 | //If run as script just output patched file 11 | if (require.main === module) 12 | console.log(source); 13 | else { 14 | var _module = new Module('aesprim'); 15 | _module._compile(source, filename); ^ TypeError: _module._compile is not a function. (In '_module._compile(source, filename)', '_module._compile' is undefined) at /home/jaysingh/projects/test/react-test/node_modules/jsonpath/lib/aesprim.js:15:2 at globalThis (/home/jaysingh/projects/test/react-test/node_modules/jsonpath/lib/aesprim.js:14:6) at require (:1:20) at /home/jaysingh/projects/test/react-test/node_modules/jsonpath/lib/handlers.js:1:4 at globalThis (/home/jaysingh/projects/test/react-test/node_modules/jsonpath/lib/handlers.js:260:17) at require (:1:20) at /home/jaysingh/projects/test/react-test/node_modules/jsonpath/lib/index.js:4:4 at globalThis (/home/jaysingh/projects/test/react-test/node_modules/jsonpath/lib/index.js:249:17) error: script "build" exited with code 1 (SIGHUP)

Additional information

No response

posabsolute commented 1 year ago

Same issue here, i think jsonpath is not supported on Bun

bobkorinek commented 1 year ago

It looks like Bun doesn't define _compile method for Module class which is used by jsonpath library (I don't know Bun source code so this is a very long shot, but my guess is this code section where this method could be defined, but currently it's not). You can find the definition of _compile method in NodeJS, so that's the reason why it's working there.

PS: I found out about this problem in my CI pipeline, where I use Docker image of Bun, but when I tried to run Bun on my local machine, where I have NodeJS, Bun and other tools installed, it worked just fine.

mternisien commented 1 year ago

Any plans to define _compile method into bun ? Thx in advance.

scythargon commented 1 year ago

Following. Just stumbled upon the same issue with jsonpath.

maugenst commented 1 year ago

Same here. To reproduce this:

mkdir buntest
cd buntest
bun init
bun i jsonpath

Edit index.ts

import jp from 'jsonpath';
const cities = [
  { name: 'London', population: 8615246 },
  { name: 'Berlin', population: 3517424 },
  { name: 'Madrid', population: 3165235 },
  { name: 'Rome', population: 2870528 },
];

const names = jp.query(cities, '$..name');

console.log(names);

Run

$ bun run index.ts
10 | //If run as script just output patched file
11 | if (require.main === module)
12 |   console.log(source);
13 | else {
14 |   var _module = new Module('aesprim');
15 |   _module._compile(source, __filename);
      ^
TypeError: _module._compile is not a function. (In '_module._compile(source, __filename)', '_module._compile' is undefined)
      at /Users/d032233/github/buntest/node_modules/jsonpath/lib/aesprim.js:15:2
      at globalThis (/Users/d032233/github/buntest/node_modules/jsonpath/lib/aesprim.js:14:6)
      at require (:1:20)
      at /Users/d032233/github/buntest/node_modules/jsonpath/lib/handlers.js:1:4
      at globalThis (/Users/d032233/github/buntest/node_modules/jsonpath/lib/handlers.js:260:17)
      at require (:1:20)
      at /Users/d032233/github/buntest/node_modules/jsonpath/lib/index.js:4:4
      at globalThis (/Users/d032233/github/buntest/node_modules/jsonpath/lib/index.js:249:17)
danlobo commented 1 year ago

Same here, similar stacktrace with jsonpath involved.

mwmercury commented 1 year ago

For everyone who came here just to say "following" or "same here": I don't mean to be rude, but you can simply press the subscribe button instead. Thank you.

JustRedTTG commented 1 year ago

I can confirm this bug occurs when making a oven/bun docker container and installing packages and then running bun run build I tried that outside of docker and it works fine. I also tried modifying what command arguments I used for bun install and it seemed to have no effect

JustRedTTG commented 1 year ago

I found the issue and it seems like bun is automatically running with --bun flag? I tried to run build script on my machine which usually works, but with --bun exact same error as mentioned above!

bobkorinek commented 1 year ago

@JustRedTTG The reason why it works on you local machine is bacause Bun executes some scripts using Node.

It's common for package.json scripts to reference locally-installed CLIs like vite or next. These CLIs are often JavaScript files marked with a shebang to indicate that they should be executed with node.

So if you have Node installed on your machine Bun will try to execute files containing shebang using Node. But if you use --bun it's going to run every script with Bun.

By default, Bun respects this shebang and executes the script with node. However, you can override this behavior with the --bun flag. For Node.js-based CLIs, this will run the CLI with Bun instead of Node.js.

This is why Bun might work on your local machine, but won't work in Docker, where Node is not installed. Bun is currently not implementing _compile method for Module so that is why with pure Bun some of the code won't work. But if you have Node and execute a code containg _compile using Bun (without --bun), it might execute the _compile method with Node so it will work.

maugenst commented 1 year ago

@bobkorinek Got the idea and thanks for your explanation. As far as I can see bun is meant to be a drop-in-replacement for node. So this truly is a topic to be solved. It simply doesn't make sense to have both runtimes installed in parallel to get your code running.

bobkorinek commented 1 year ago

@maugenst Yes, I agree with you. I admire the developers behind Bun, it's not an easy task as you can see. Another thing is that methods in Node statring with _ shouldn't be used outside of Node's code (I assume), but some of the libraries use them anyway (e.g. jsonpath using _compile). For this reason making Bun 100% compatible is really hard task.