oven-sh / bun

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

issue with import / require statements #382

Closed arpitsatyal closed 2 years ago

arpitsatyal commented 2 years ago

I have simple program where I'm importing the date-fns library. even when I use the import statement, it gives me the following error:

Error: var { default: requiredArgs} = require($1e2711fb); ^ ReferenceError: Can't find variable: require

Program:

// const { add, format } = require('date-fns');
import { add, format } from "date-fns";

const today = new Date();

const nextMonth = add(today, { months: 1 }).setDate(5);
const aja = today.setDate(5);
console.log("next month is", format(nextMonth, "MMMM dd, yyyy"));
console.log("aja", format(aja, "MMMM dd, yyyy"));
ecwyne commented 2 years ago

I have the same issue (ironically also with date-fns)

Edit: This works for me when using require but NOT when using import

paperdave commented 2 years ago

i noticed this with fast-equals too. in my case, it seems to be an issue with a file that bun thinks is commonjs, but is actually an es module. in fast-equals, the file that gets imported is a .js file, inside of a package without "type": "module", and adding that line to it's package.json fixes the import. hope that helps find the actual bug.

jimmed commented 2 years ago

i noticed this with fast-equals too. in my case, it seems to be an issue with a file that bun thinks is commonjs, but is actually an es module. in fast-equals, the file that gets imported is a .js file, inside of a package without "type": "module", and adding that line to it's package.json fixes the import. hope that helps find the actual bug.

I'm having the same issue, but with the wretch library. Adding "type": "module" in wretch's package.json fixes it.

abulka commented 2 years ago

I'm having the same issue with the Tonaljs library

bun a @tonaljs/tonal
bun play.ts

gives the error ReferenceError: Can't find variable: require where play.ts is

import * as Tonal from "@tonaljs/tonal";
const chordObj = Tonal.Chord.get('Cmaj7')
console.log(chordObj)

Interestingly, adding "type": "module" in package.json does not fix it.

Same problem if the file is play.js.

thebearingedge commented 2 years ago

I just ran into a possibly-related issue. When I do this:

// index.tsx
import { renderToString } from 'preact-render-to-string'
import Document from './client/document'

I get a similar ReferenceError doing bun [run] index.tsx

1 | 
2 | var { options: e, Fragment: t} = require($e7ab4181);
   ^
 ReferenceError: Can't find variable: require
      at /path/to/node_modules/preact-render-to-string/dist/index.mjs:1:0

However, the code within the referenced file contains no require at all.

Then I added an import and from a commonjs package (and referenced it in the code to prevent it from being ignored?). No error!

// index.tsx
import ReactDOM from 'react-dom/server'
import { renderToString } from 'preact-render-to-string'
import Document from './client/document'

const { renderToString: r } = ReactDOM

So as an experiment I just created a commonjs module. And found that I could just reference module to ... "trigger" something?

// commonjs-patch.js
module;
// index.tsx
import './commonjs-patch'
import { renderToString } from 'preact-render-to-string'
import Document from './client/document'

No errors again 🤷‍♀️ . I am not smart enough to understand what is happening 😂

EDIT: Looks like if I import directly from the commonjs distribution of preact-render-to-string the script starts without error, but then segfaults when trying to execute renderToString().

EDIT EDIT: https://github.com/oven-sh/bun/issues/117#issuecomment-1031037550 So module triggers CJS transformation, but checking the referenced file again shows no module 🤔

abulka commented 2 years ago

Thanks for the fix - I simply did bun upgrade (taking me from version 0.1.2 -> 0.1.5) and tried running my project again, which uses @tonaljs/tonal. Works now!