Hi, I’m having a similar issue.
I’m developing a lambda locally using this command (through Yarn):
netlify-lambda serve src/functions --config webpack.functions.js
(to add nodeExternals
because I’m using Firebase).
I also have a .babelrc
in my functions dir to make typescript work.
All is great both locally and on Netlify.
Now I would like to embark PDF files that I want to open with fs.readFileSync
.
Initially, I had this error:
ENOENT: no such file or directory
Then I used require.resolve
as suggested, but I got:
WARNING in ./pdf/coupon.pdf 1:0
Module parse failed: Unexpected token (1:0)
So I guessed Webpack was trying to load it as a source file, so I updated my webpack.functions.js
like this:
const nodeExternals = require("webpack-node-externals")
module.exports = {
externals: [nodeExternals()],
module: {
rules: [
{
test: /\.pdf$/i,
use: [{ loader: "file-loader" }],
},
],
},
}
But now the error is:
{ Error: ENXIO: no such device or address, read
at Object.readSync (fs.js:493:3)
at tryReadSync (fs.js:332:20)
at Object.readFileSync (fs.js:369:19)
at Object.b [as handler] ([...]/functions/coupon.js:1:4032)
at process._tickCallback (internal/process/next_tick.js:68:7) errno: -6, syscall: 'read', code: 'ENXIO' }
Any clue?
Edit: I realized that using a Babel loader meant the file would actually be read, so I don’t need to read it again, right? So I just imported the PDF:
const pdf = require("./assets/template.pdf")
(import
works too)
but now the pdf-lib
is failing with an error:
Error: Failed to parse PDF document (line:0 col:54 offset=27): No PDF header found
But if I make a Node script to open that PDF with that lib, it works…