Do serverless functions support npm package ? If yes how to embbed it

Hello,

I want to generate PDF in a serverless background function. I did a POC on a classic node js project using this package pdf-creator-node - npm (Note that this package have a dependency to another named “html-pdf”). In the classic node js project, everything works like a charm.

But in my netlify serverless function, I get this message :

Error: html-pdf: Unknown Error
Can't open 'C:\devroom\my-project\pdf-generation\.netlify\functions-serve\creation\src\netlify\functions\creation\scripts\pdf_a4_portrait.js'

  't open 'C:\devroom\my-project\pdf-generation\.netlify\functions-serve\creation\src\netlify\functions\creation\scripts\pdf_a4_portrait.js'
  
  ChildProcess.respond (C:\devroom\my-project\pdf-generation\node_modules\html-pdf\lib\pdf.js:134:17)
  ChildProcess.emit (events.js:400:28)
  ChildProcess.emit (domain.js:470:12)
  Process.ChildProcess._handle.onexit (internal/child_process.js:277:12)

I try to make a “npm init” command and add the package in the root of the project and in the function folder but I still get this message.

So my questions are, is-it possible to use npm package ? If yes what are the steps to follow ? Is my issue from my setup or from the package ? If yes which one :joy: , “pdf-creator-node” or his dependency “html-pdf” ?

Regards,

I’m not sure what you’re doing. Could you share an example of your attempt? This error is more or less of not much use in its current form. “Unknown error” is not something we can debug.

It says Can’t open your JavaScript file. Why are you trying to open the file from /.netlify/functions?

There are a lot of questions about your setup, so instead of making guesses, we would like to see a reproduction.

Hello,
I want to use an npm package. But I don’t know if I have to do “npm install xx” inside the functions/my-function folder, or inside the root folder.

Ok I understand better my problem. A dependency of one of my package build a script path. This script path is good in a classic node project. But do not work on the .netlify/function-serve folder.

Look here : node-html-pdf/pdf.js at 7e8c02af936341c1ba23a6a1c422649363e7a7aa · marcbachmann/node-html-pdf · GitHub


How can I say to netlify to copy too the missing file in the folder please ?

Steps to reproduce :

  • Create a netlify function project
  • Add the package pdf-creator-node - npm
  • Try to generate pdf from a serverless function
  • I added to netlify.toml this :
[functions] 
    node_bundler = "esbuild" 
    included_files = [
        "template/**", 
        "core/**", 
        "model/**", 
        "node_modules/**",
        ]
  

I resolve my issue by adding the path of missing script in option object parameter of the module
script: “node_modules/html-pdf/lib/scripts/pdf_a4_portrait.js”