Useage of contrib-npm

Hi!

I'd like to use the npm node (node-red-contrib-npm (node) - Node-RED) to be able to use pdf-merger-js (pdf-merger-js - npm).

Unfortunately, I always get errors like TypeError: func is not a function or similar, depending on how I connect the individual nodes in the following flow:

[{"id":"ae6cb40ae0efa2ed","type":"npm","z":"4b453fb457938d95","name":"pdf-merger-js","func":"const PDFMerger = require('pdf-merger-js');\n\nvar merger = new PDFMerger();\n\n(async () => {\n  await merger.add('pdf1.pdf');  //merge all pages. parameter is the path to file and filename.\n  await merger.add('pdf2.pdf');\n\n  await merger.save('merged.pdf'); //save under given name and reset the internal document\n})();\nreturn npm_module(msg.payload);","npm_module":"pdf-merger-js","module_style":"custom","msg_payload":"return_val","function_name":"","x":470,"y":600,"wires":[["07e72953e011a95c"]]},{"id":"24b90b147213d017","type":"inject","z":"4b453fb457938d95","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":600,"wires":[["ed2cb1df163e8109"]]},{"id":"07e72953e011a95c","type":"debug","z":"4b453fb457938d95","name":"debug 36","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":660,"y":560,"wires":[]},{"id":"ed2cb1df163e8109","type":"npm","z":"4b453fb457938d95","name":"pdf-lib","func":"// NPM module exposed as variable, npm_module\nreturn npm_module(msg.payload);","npm_module":"pdf-lib","module_style":"cstr","msg_payload":"callback_error","function_name":"","x":230,"y":540,"wires":[["102cd5d700f07c78"]]},{"id":"102cd5d700f07c78","type":"npm","z":"4b453fb457938d95","name":"pdf-merger-js install","func":"// NPM module exposed as variable, npm_module\nreturn npm_module(msg.payload);","npm_module":"pdf-merger-js","module_style":"cstr","msg_payload":"return_val","function_name":"","x":430,"y":540,"wires":[["ae6cb40ae0efa2ed"]]}]

Any idea how I am able to get this to work?

I'd also appreciate another solution on how I would be able to generate a template output per injected payload to store each output as a page in one file using node-red-contrib-pdf (node) - Node-RED instead of the approach of combining the resulting pdfs.

Thanks!

I'd like to use the npm node

You don't have to use that node anymore, it has been integrated into the function node into the setup tab.

Thanks, but this still requires me to add it to settings.js or am I wrong? (Writing Functions : Node-RED)

It needs to be enabled in settings.js:

setting functionExternalModules to true in you settings.js file

restart node-red and you should be able to use any npm package within the function node.

hm this is already set to true.

I added the modules:

On message now looks:

const PDFMerger = require('pdfMergerJs');

var merger = new PDFMerger();

(async () => {
  await merger.add('pdf1.pdf');  //merge all pages. parameter is the path to file and filename.
  await merger.add('pdf2.pdf');

  await merger.save('merged.pdf'); //save under given name and reset the internal document
})();
return msg.payload;

Getting the error:

ReferenceError: require is not defined (line 1, col 19)

Should it be requrire('pdf-merger-js'); (original node name) or requrie('pdfMergerJs') from "Import as" in module settings?
Or what am I missing here?

Thanks

You can remove the require, as that is what the setup tab invokes.

const merger = new pdfMergerJs()

1 Like

You are my absolute hero.

Her is the final, working code:

const merger = new pdfMergerJs();

(async () => {
  await merger.add('/media/file1.pdf');  //merge all pages. parameter is the path to file and filename.
  await merger.add('/media/file2.pdf');

  await merger.save('/media/merged.pdf'); //save under given name and reset the internal document
})();
return msg

As I need to change the function based on a list of files in a folder (and I need support with that but it has nothing to do with contrib-npm), I'll create a new topic on that.

Thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.