sveltekit-1.0-sqlite-demo-app icon indicating copy to clipboard operation
sveltekit-1.0-sqlite-demo-app copied to clipboard

PDF generation not working on production

Open lapoguidi opened this issue 1 year ago • 2 comments

Testing in production with adapter-node gettings

ReferenceError: __dirname is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and 'package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at requireFontkit (file:///home/ubuntu/build/server/chunks/_server-17432636.js:39657:45)
    at file:///home/ubuntu/build/server/chunks/_server-17432636.js:64668:22
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

The error occurs here: var trie = new UnicodeTrie(fs.readFileSync(__dirname + '/data.trie')); If this occurred also for you, how have you solved this?

Thanks

Lapo

lapoguidi avatar Jul 02 '23 21:07 lapoguidi

Ok, weird; it works for me. How are you running the production build, and what is your node version?

From the error message, it seems like there is some ESM <-> CommonJS mismatch. I used __dirname nowhere, so it has to be in some dependency.

phartenfeller avatar Jul 03 '23 15:07 phartenfeller

Hello, thanks for reply. I tried with 18 LTS: v18.6.0 then I upgraded to v18.6.2.

The error occurs when pdfmake is imported.

Anyway __dirname is used in a pdfKit dependency: https://github.com/foliojs-fork/fontkit

var trie = new UnicodeTrie(fs.readFileSync(__dirname + '/data.trie'));

I'm looking for a workaround : )

lapoguidi avatar Jul 03 '23 22:07 lapoguidi