oven-sh / bun

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

bun build --compile error #14509

Open youth95 opened 3 hours ago

youth95 commented 3 hours 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 2 hours ago

Latest node doesn't support the require('axios/dist/node/axios.cjs');

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 

And also if you make bun runtime run it outside the try catch, it will fail too. So you should remove this catch import as you will only be using bun here and then it won't have to try to import.


If you can modify the code and need to have this, you can use --define with a check to make bun not bother:

if (!process.versions.bun)
    axios = require('axios/dist/node/axios.cjs');
$ bun build path/to/main.ts --compile --target=bun-linux-x64 --outfile=app --minify --sourcemap --define process.versions.bun=true