oven-sh / bun

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

bun build --compile error #14509

Open youth95 opened 1 month ago

youth95 commented 1 month ago

What version of Bun is running?

1.1.31

What platform is your computer?

Darwin 23.5.0 arm64 arm

What steps can reproduce the bug?

some code in my dependencies packages look like this:

/**
 * Axios uses ES Module exports,
 * Node.js versions < 12 do not support importing ES Modules with *.js,
 * so for compatibility, we use `require` to import Axios.
 */
/* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires */
let axios;
try {
    /**
     * Node.js versions that support importing ES Modules with *.js.
     */
    axios = require('axios');
}
catch (_a) {
    /**
     * Node.js versions that support importing CommonJS Modules with *.cjs.
     * Additionally, since Node.js versions v14.13.0 and v12.20.0 have enabled the conditional exports feature [1], this import method cannot be used.
     *
     * [1]: https://nodejs.org/docs/latest/api/packages.html#determining-module-system
     */
    axios = require('axios/dist/node/axios.cjs');
    /*
     * There are no Node.js versions that both `do not support *.js importing ES Modules` 
     * and `have enabled the conditional exports feature`, 
     * so no exceptions will be thrown here.
     */
}

when i run this command:

bun build path/to/main.ts --compile --target=bun-linux-x64 --outfile=app --minify --sourcemap

i had an error:

30 |     axios = require('axios/dist/node/axios.cjs');
                         ^
error: Could not resolve: "axios/dist/node/axios.cjs". Maybe you need to "bun install"?

is it not supported require('path/to/file.cjs') when bun build to dependencies analysis ?

thank you

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

RiskyMH commented 1 month ago

It seems that the issue arises from using require('axios/dist/node/axios.cjs'), which is not supported in the latest version of Node.js due to the way exports are defined in the axios package. The error you're seeing is related to subpath exports not being exposed in axios.

Node: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './dist/node/axios.cjs' is not defined by "exports" in C:\bun-repos\14509\node_modules\axios\package.json 

For context, bun build tries to bundle every import, assuming they’ll be needed. In a normal runtime, this import wouldn’t be encountered, but during the build process, Bun attempts to bundle it and encounters this issue.


Unfortunately, since this isn't your code, there's not much you can do besides patching the axios package.json or your library. You can patch it using bun patch.