bun icon indicating copy to clipboard operation
bun copied to clipboard

Error with `pdf-text-reader` and NODE_ENV=production

Open randompixel opened this issue 11 months ago • 4 comments

What version of Bun is running?

1.0.35+940448d6b

What platform is your computer?

Darwin 23.2.0 arm64 arm

What steps can reproduce the bug?

If I attempt to use pdf-text-reader (which itself uses Mozilla's pdf.js and this is where the error is happening) when not in dev mode it fails.

  • Developed in bun run dev = works fine
  • Ran bun build src/index.ts --outdir ./out --target bun --sourcemap=external and then bun run out/index.js = fail

So I switched the oven/bun docker build I have to call bun run dev directly instead of building it. Except that also failed when I went into production. After a lot more trial & error

  • ENV NODE_ENV=production I commented out this line from the dockerfile and it works.

So it appears that build also sets NODE_ENV=production but then can't resolve require when it does

Minimal reproduction:

import { readPdfText } from "pdf-text-reader";

/** Define the return type for a FileParser class' parse function */
type ParsedFile = {
	fileName: string;
	fileSize: number;
	fileType: string;
	body: string;
	footers?: string;
	headers?: string;
};

/** Define the signature for a FileParser class */
type ParseFunction = (file: File) => Promise<ParsedFile>;

/** Define the type of classes that the factory can return */
interface FileParser {
	parse: ParseFunction;
}

class PdfParser implements FileParser {
	public async parse(file: File): Promise<ParsedFile> {
		const blob = file;
		const stream = await blob.arrayBuffer();
		const readText = await readPdfText({ data: stream, worker: null });

		return {
			fileName: blob.name,
			fileSize: blob.size,
			fileType: blob.type,
			body: readText,
		};
	}
}

Bun.serve({
  port: 4000,
  async fetch(req) {
    const url = new URL(req.url);

    // parse formdata at /action
    if (url.pathname === '/parse') {
      const formdata = await req.formData();
      const file = formdata.get('file');
			console.log(file);
			const parser = new PdfParser();
			const body = await parser.parse(file);
			return new Response(body.body);
		}

  
		return new Response("Not Found", { status: 404 });
	}
});

What is the expected behavior?

POST a file through form-data and it parses the text out of the PDF when running in NODE_ENV=production

What do you see instead?

Setting up fake worker failed: \"Can't find variable: require\"

Additional information

No response

randompixel avatar Mar 26 '24 16:03 randompixel