New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bun build --target node
injects incorrect require
function
#6168
Comments
@edi-manrique Try changing the extension to .mjs and run it |
|
@edi-manrique I had the same issue, here is a workaround to make it work with NodeJS (found in this comment on the following ticket #5707) You have to add this line on top of your generated/built JS file: import { createRequire as createImportMetaRequire } from "module"; import.meta.require ||= (id) => createImportMetaRequire(import.meta.url)(id); I wrote a custom import path from 'path';
import fs from 'fs';
// NodeJS Build
const NODE_FIX = 'import { createRequire as createImportMetaRequire } from "module"; import.meta.require ||= (id) => createImportMetaRequire(import.meta.url)(id);\n';
const BUILD_DIR = 'build';
const nodeBuild = await Bun.build({
entrypoints: ['./index.ts'],
target: 'node',
minify: true,
naming: "[dir]/[name].[ext]"
});
// Write output files
for (const result of nodeBuild.outputs) {
const fileContent = NODE_FIX + await result.text();
const destDir = path.join(import.meta.dir, BUILD_DIR);
const dest = path.join(destDir, result.path);
fs.existsSync(destDir) || fs.mkdirSync(destDir);
Bun.write(dest, fileContent);
} Then you just have to run: bun run build.js Adapt it to your needs, hope it will help. |
To automate adding import { createRequire as createImportMetaRequire } from "module"; import.meta.require ||= (id) => createImportMetaRequire(import.meta.url)(id); to the generated file while considering hashbangs, you can add this to your scripts in package.json "postbuild": "npx rexreplace '^(#!.+\\n)?' '$1import { createRequire as createImportMetaRequire } from \'module\'; import.meta.require ||= (id) => createImportMetaRequire(import.meta.url)(id);\\n' -GM path/to/YOUR_FILE.js", |
import.meta.require
in bundled code when building with target: "node"
#5154
I have the same problem, is there a better solution |
@soeasyjx I don't think so. |
bun build --target node
errorbun build --target node
injects incorrect require
function
@mathiasrw really liked the workaround, but I am getting this issue for some reason...
Am I missing something? I'm very new to Bun, and a bit new to bundlers in general. |
@augustoapg, escape quotes arround 'module' ?
|
@fradinni ohhh - good point. I have updated the example. |
@fradinni peerfect, that did it! Thank you so much! And thanks @mathiasrw again for the clever solution! |
I reported this error after executing package.json{
"start": "bun run src/index.ts",
"build":"bun build src/index.ts --outdir dist --target node"
}
ReferenceError: Bun is not defined |
@soeasyjx Please make new issues when you encounter a new issue. also: You cant use |
I am experiencing this as output from bun build with
the actual |
What version of Bun is running?
1.0.3+25e69c71e70ac8a0a88f9cf15b4057bd7b2a633a
What platform is your computer?
Darwin 21.6.0 x86_64 i386
What steps can reproduce the bug?
What is the expected behavior?
What do you see instead?
Additional information
The text was updated successfully, but these errors were encountered: