We were using html-pdf library earlier to generate PDF from html. The library is now deprecated and puppeteer was suggested.
We made the code changes and it worked fine on localhost (Windows 11) machine.
But when we tried to do npm install on server (Ubuntu 18), it crashed with this error:
npm ERR! code ENOMEMnpm ERR! A complete log of this run can be found in: /root/.npm/_logs/2023-09-01T14_19_37_157Z-debug-0.log1263 info run puppeteer@20.9.0 postinstall node_modules/puppeteer node install.js1264 info run puppeteer@20.9.0 postinstall { code: undefined, signal: undefined }1265 verbose stack Error: spawn ENOMEM1265 verbose stack at ChildProcess.spawn (node:internal/child_process:420:11)1265 verbose stack at spawn (node:child_process:733:9)1265 verbose stack at /root/.nvm/versions/node/v16.20.0/lib/node_modules/npm/node_modules/@npmcli/promise-spawn/lib/index.js:18:121265 verbose stack at new Promise (<anonymous>)1265 verbose stack at promiseSpawn (/root/.nvm/versions/node/v16.20.0/lib/node_modules/npm/node_modules/@npmcli/promise-spawn/lib/index.js:17:13)1265 verbose stack at spawnWithShell (/root/.nvm/versions/node/v16.20.0/lib/node_modules/npm/node_modules/@npmcli/promise-spawn/lib/index.js:123:10)1265 verbose stack at promiseSpawn (/root/.nvm/versions/node/v16.20.0/lib/node_modules/npm/node_modules/@npmcli/promise-spawn/lib/index.js:12:12)1265 verbose stack at runScriptPkg (/root/.nvm/versions/node/v16.20.0/lib/node_modules/npm/node_modules/@npmcli/run-script/lib/run-script-pkg.js:79:13)1265 verbose stack at runScript (/root/.nvm/versions/node/v16.20.0/lib/node_modules/npm/node_modules/@npmcli/run-script/lib/run-script.js:9:16)1265 verbose stack at Array.<anonymous> (/root/.nvm/versions/node/v16.20.0/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/rebuild.js:363:17)
Here is our code to generate pdf:
import puppeteer from 'puppeteer';export class PdfUtil {constructor() {}public writePdf(filePath: any, htmlData: any) { return new Promise(async (resolve, reject) => { try { const browser = await puppeteer.launch({ headless: 'new' }); const page = await browser.newPage(); await page.setContent(htmlData); await page.emulateMediaType('screen'); const pdfOptions = { printBackground: true, path: filePath, // The path where the PDF will be saved margin: { top: '1cm', right: '1cm', bottom: '1cm', left: '1cm' }, // Optional page margins }; await page.pdf(pdfOptions); await browser.close(); resolve(true); } catch (error) { reject(error); } });}}
Environment:
Node Version = 16.20.0"puppeteer": "^20.9.0""typescript": "^5.0.4"
What is the issue?
Edit
I freed and increased the memory and now on npm install, I'm getting this error:
Error: Cannot find module 'puppeteer-core/internal/puppeteer-core.js'