I have a bunch of lambdas and want to attach a layer containing several node packages.
The lambda is finding the code, so I know the path is correct.
I think my question is how to call a constructor within the layer from my index.mjs lambda module.
Any help much appreciated,
David
Lambda Code:
var FPDF = import('/opt/node_modules/node-fpdf/src/fpdf_js.js');
export const handler = async (event) => {
console.log(JSON.stringify(event));
var pdf = new FPDF('P','mm','A4');
pdf.AddPage();
pdf.SetFont('Arial','B',12);
pdf.Cell(5,5,"Hello World!!");
pdf.Output('F',`test.pdf`);
}
Code in node_modules/node-fpdf/src/fpdf_js.js:
module.exports = class FPDF {
constructor(orientation = 'P', unit = 'mm', size = 'A4', PDFA = false, posX = 1, posY = 1) {
....
}
SetFont(family, style = '', size = 0) {
....
}
....
}
I've tried the following:
import { FPDF } from '/opt/node_modules/node-fpdf/src/fpdf_js.js';
var pdf = new FPDF('P','mm','A4');
**SyntaxError: Named export 'FPDF' not found. The requested module '/opt/node_modules/node-fpdf/src/fpdf_js.js' is a CommonJS module, which may not support all module.exports as named exports. CommonJS modules can always be imported via the default export, for example using: import pkg from '/opt/node_modules/node-fpdf/src/fpdf_js.js'; const { FPDF } = pkg;**
import pkg from '/opt/node_modules/node-fpdf/src/fpdf_js.js';
const { FPDF } = pkg;
var pdf = new FPDF('P','mm','A4');
**TypeError: FPDF is not a constructor**
var FPDF = require('/opt/node_modules/node-fpdf/src/fpdf_js.js');
var pdf = new FPDF('P','mm','A4');
**require is not defined in ES module scope, you can use import instead**
var FPDF = import('/opt/node_modules/node-fpdf/src/fpdf_js.js').FPDF;
var pdf = new FPDF('P','mm','A4');
**TypeError: FPDF is not a constructor**
var FPDF = import('/opt/node_modules/node-fpdf/src/fpdf_js.js').default();
var pdf = new FPDF('P','mm','A4');
**ImportCall("/opt/node_modules/node-fpdf/src/fpdf_js.js").default is not a function**
I would look into commonJS vs ES syntax for importing... As you're mixing and matching the two and most of them are plain old JS errors.
You're exactly right Dakota - I guess it's the penalty we pay for re-use :) It seems layers can be a mixture of old and new (good) but that means the access has to be done both ways (bad if like me you're still a JS learner :) )