How to use Blob in n8n

@jan @MutedJam @RicardoE105
Hi,
How to use Blob in n8n, I’m getting error while using Blob
Code:

  const blob = new Blob([byteArray], {type: contentType});

Error:

Blob:Cannot not undefined

Hey @AMS_KHADHIRI,

Is that the full error you get? It looks like something could be missing.

@Jon This is the exact error I;m getting

@shrey-42 Do you have any idea?

It looks like you need to import/required first.

1 Like

@RicardoE105 This is not working, Do you have any alternative?

Hey @AMS_KHADHIRI,

What error are you getting when trying? You may need to set the NODE_FUNCTION_ALLOW_BUILTIN environment option or the external one and see if that helps.

Looking at the Node docs there could be a bit more to it: Buffer | Node.js v16.14.2 Documentation

@jan If I use this code in n8n getting error

  const blob = new buffer.Blob([byteArray], {type: contentType});

Error:-

@Jon I’m getting error while using ‘cross-blob’

Have you set NODE_FUNCTION_ALLOW_BUILTIN Maybe and if using the external node package have you added it to your install and told n8n it can use it?

What are you actually trying to do with the blob data there may be another way to handle it with less messing around.

I’m trying to upload pdf content as Blob to our rest-end point. Rest-end point allow binaryData only.

Have you tried just posting the binary data to it to see what happens?

Our API supports only blob type data. If I try to pass directly binary data api returns validation error.

From what I understand blobs are more for the browser than Node iteself and the solution normally is to pass the binary data to get it working.

In your case if a Blob is the only option you have you will probably need to install a blob package into your n8n image (or if using npm you can install one like normal). From there as long as the package isn’t for the browser you should be good to go.

Is it your own API you are trying to post to or a third party API?

We have installed two packages already cross-blob and buffer that didn’t help us.

const {Blob} = require('cross-blob');

I have installed cross-blob on docker.
If you know any good node module for blob. Please suggest.

Hey @AMS_KHADHIRI,

Can you tell me exactly what steps you took when installing Cross Blob, That might shed some light on this one as at the moment I have lots of little pieces of information but not everything.

Looking back at the error message it could be that you don’t have the module in the right path.

I am working with @AMS_KHADHIRI and this is how it is configured,

The environment variable values are:

{
  "name": "NODE_FUNCTION_ALLOW_BUILTIN",
  "value": "crypto"
},
{
  "name": "NODE_FUNCTION_ALLOW_EXTERNAL",
  "value": "axios,request-promise-native,form-data,cross-blob,buffer"
},

The n8n Docker file has the following stuff,

FROM n8nio/n8n:0.171.0

ENV EXECUTIONS_DATA_PRUNE=true
# 1440 hours is 60 days
ENV EXECUTIONS_DATA_MAX_AGE=1440
# 3600 (one hour) is how often N8N will attempt to prune old data
ENV EXECUTIONS_DATA_PRUNE_TIMEOUT=3600
# There is no timeout by default, this 3600 (one hour) setting will kill stuck workflows
ENV EXECUTIONS_TIMEOUT=3600

# We need cross-blob to deal with incoming email attachments that are
# binary large objects (blobs).
RUN npm install --global cross-blob

Note that we are installing the cross-blob module inside the docker image.

Hey @vasu-fielda,

Thanks for the extra information, can you check in the docker container that the npm package is installed and available under the n8n/node_modules folder?

The error would suggest it isn’t there, I have given it a quick test on my install and once in the correct folder it does load but that package seemed to give me other issues when I tried to use it but I suspect as it is one you are using you will know how to use it.

const axios = require('axios');
const FormData = require('form-data');
const Blob = require('cross-blob');

const formData = new FormData();
const token=$node["Auth_Token"]?.json?.token;
const contentType='application/pdf';

function b64toBinary(b64Data){
  const byteCharacters = Buffer.from(b64Data, 'base64').toString('binary');
  const byteNumbers = new Array(byteCharacters.length);
  
  for (let i = 0; i < byteCharacters.length; i++) {
      byteNumbers[i] = byteCharacters.charCodeAt(i);
  }
  const byteArray = new Uint8Array(byteNumbers);
  const blob = new Blob([byteArray], {type: contentType});
   return blob;

}

formData.append('pdf',b64toBinary($node["Set"]?.json?.pdf));
formData.append('Id', activityId);
formData.append('Key', key);
formData.append('sha1',sha1);
const response2= await axios.post('apiURL', formData, {
  headers: {
    'Content-Type': 'multipart/form-data',
    // eslint-disable-next-line no-undef
    Authorization: `Bearer ${token}`,
    Accept: 'application/json',
  },
}).then((response) => console.log(response).catch((error) => console.log(error)));


return response2;


This is what we are trying to do upload the pdf file. This code works outside of the n8n.

We used to get error when cross-blob

ERROR: require() of ES Module /usr/local/lib/node_modules/cross-blob/index.js from /usr/local/lib/node_modules/n8n/node_modules/vm2/lib/resolver-compat.js not supported. Instead change the require of index.js in /usr/local/lib/node_modules/n8n/node_modules/vm2/lib/resolver-compat.js to a dynamic import() which is available in all CommonJS modules. [Line 3]