What I did:
a) install a docker container with a Debian OS and Node version 20.8.1
b) install in the container nodered npm install -g node-red
c) declare nodered as a service to be started on container start with systemctl
d) install "mupdf" with npm install mupdf-js in the node-red folder
e) modify settings.js section "functionGlobalContext" as follows:
// The following property can be used to seed Global Context with predefined
// values. This allows extra node modules to be made available with the
// Function node.
// For example,
// functionGlobalContext: { os:require('os') }
// can be accessed in a function block as:
// global.get("os")
functionGlobalContext: {
mupdfJS:require('mupdf-js')
// os:require('os'),
// jfive:require("johnny-five"),
// j5board:require("johnny-five").Board({repl:false})
},
f) restart NodeRed
g) Create a function node
g1) Declare "mupdf-js" as "mupdfJS" in the "SETUP" tab
g2) in the "On Message" tab :
const mudf = global.get('mupdfJS');
var doc = new mupdf.Document.openDocument('filename.pdf','application/pdf');
msg.payload = doc;
return msg;
When launched, the flow returns TypeError: Cannot read properties of undefined (reading 'Document')
The logs of the Nodered service only showed:
[error] [function:muPDF] TypeError: mupdfJS is not a function
var file = msg.payload; // the pdf file is loaded with prior "read-file" node as a buffered array
const mupdf = mupdfJs.createMuPdf();
const buf = file.arrayBuffer();
const doc = mupdf.load(buf);
msg.payload = doc;
return msg;
Try adding mupdf-js to function node setup. Name it mupdfJs then see what it contains by doing node.warn({mupdfJs})
It is likely you will see mupdfJs contains createMuPdf as an object or function. Just expand it out in the debug node until you see createMuPdf as a function - that'll be what you use.
And in case it is not clear, import and require are not possible directly in the function node code. That's what the setup tab does.
It unfortunately resulted in a server crash again. Logs gave:
24 Oct 07:37:53 - [warn] Encrypted credentials not found
24 Oct 07:37:53 - [info] Server now running at http://127.0.0.1:1880/
24 Oct 07:37:53 - [info] Starting flows
24 Oct 07:37:53 - [info] Started flows
24 Oct 07:38:54 - [info] Stopping flows
24 Oct 07:38:54 - [info] Stopped flows
24 Oct 07:38:54 - [info] Updated flows
24 Oct 07:38:54 - [info] Starting flows
24 Oct 07:38:54 - [info] Started flows
TypeError: Failed to parse URL from /root/.node-red/node_modules/mupdf-js/dist/libmupdf.wasm
TypeError: Failed to parse URL from /root/.node-red/node_modules/mupdf-js/dist/libmupdf.wasm
24 Oct 07:38:57 - [red] Uncaught Exception:
24 Oct 07:38:57 - [error] RuntimeError: abort(TypeError: Failed to parse URL from /root/.node-red/node_modules/mupdf-js/dist/libmupdf.wasm). Build with -s ASSERTIONS=1 for more info.
at process.abort (/root/.node-red/node_modules/mupdf-js/dist/libmupdf.js:9:13762)
at process.emit (node:events:514:28)
at emit (node:internal/process/promises:150:20)
at processPromiseRejections (node:internal/process/promises:284:27)
at process.processTicksAndRejections (node:internal/process/task_queues:96:32)
After some readings on the web, I decided to downgrade node.js version from 20.8.1 to 16.20.2. The flow doesn't crash anymore. A good start!
Have you checked this file can actually be read by the lib outside of Node-RED? (I.e. create a small node project that does the same thing - did it work on that file with current node version?)