How to use Blob in n8n

Hey @Siva_Nagarajan,

That error looks to be Node telling you that the require() for that module is not supported so it could be that another module is needed or you will need a different way to load it.

As a quick test I have ran the below in VSCode with Node to see what happens.

const Blob = require('cross-blob');
const blob = new Blob(["aaa"]);

This resulted in the error below

> node main.js          
/Users/jon/Code/javascript/test/blob/main.js:1
const Blob = require('cross-blob');
             ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/jon/Code/javascript/test/blob/node_modules/cross-blob/index.js from /Users/jon/Code/javascript/test/blob/main.js not supported.
Instead change the require of index.js in /Users/jon/Code/javascript/test/blob/main.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/jon/Code/javascript/test/blob/main.js:1:14) {
  code: 'ERR_REQUIRE_ESM'

It looks to be the same error message you are getting from n8n but I am getting it outside of n8n where you have it working, I have had a quick play with the Node buffer option and that seems to fail in n8n possibly due to the node version being used.

At the moment the quickest option I can thing of would be to use the execute command option to get your blob from another process.

If we use ‘node:buffer’ n8n throws error.

Screenshot from 2022-04-08 18-59-09

Oh! But I’m not sure how to use the execute command option to get blob from another process.

You would need to have process of some kind that could do the job. The node buffer blob option seems to fail in my testing as well but I get a different error.

In theory you could create a new node to do this for you as well but at the moment I can’t see a way to make a blob without needing something else.

Hey everyone, thanks for all the ideas and help (I’m also working with Khadhiri). :wave:

Here’s our minimal reproducible example:

console.log(`Node version: ${process.version}`);
const { Blob } = require("buffer");
const blob = new Blob(['abc']);

See also: Blob Fail Workflow · GitHub

The console prints v16.14.2, then fails with ERROR: Blob is not a constructor [Line 3]. :thinking:

TypeError: Blob is not a constructor
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function:3:14
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function:4:2
    at BaseHandler.apply (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/bridge.js:479:11)
    at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/nodevm.js:425:23)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function/Function.node.js:96:31)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:536:37)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:451:62
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

I switched to n8nio/n8n:0.171.0-debian in case Alpine Linux was missing something for Node 16, but the same failure happens on Debian too.

Class Blob is ‘Stability: 1 - Experimental’ for Node 16, but from what I can tell, it doesn’t need a command-line flag like some other experimental features; when I run const { Blob } = require("buffer"); const blob = new Blob(['abc']); in a Node 16.14.2 REPL, it works fine.

That’s all I know, for now.

Hey @robert-claypool,

I had the same issue, not sure why the buffer blob object doesn’t work and I am not sure if the problem is something we have caused or if it is some nodejs oddity.

Have you tried running the npm version of n8n instead of the docker images to see if that works? For now I can’t see this working though from within an n8n function.

What you could do is create a JavaScript file on the image and use the execute command node to run it which in theory might be able to do what you are after and return the output to n8n for the next step.

Do we have any reference articles or documentation to do this procedure?

Hey @Siva_Nagarajan,

Not that I know of, in theory it is just mounting a volume then from the execute command running node /path/to/script.js passing whatever you want.

Update: Our work-around is to save the incoming PDF with a ‘Write Binary File’ node, then HTTP POST it to another server with curl using the ‘Execute Command’ node.

curl -X 'POST' \
  '{{$node["Config"].json["url"]}}' \
  -H 'accept: application/json' \
  -H 'Authorization: Bearer {{$node["Login"].json["token"]}}' \
  -H 'Content-Type: multipart/form-data' \
  -F 'id={{$node["GetUniqueID"].json["id"]}}' \
  -F '[email protected]/home/node/.n8n/report.pdf;type=application/pdf'
1 Like

Hey @robert-claypool,

That is a good work around, I didn’t think curl sent data as a blob I thought it sent an encoded buffer a bit like our HTTP Request node.

That is good to know for the future.