Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix 'TypeError: Buffer.from is not a function'?

Problem

While trying to use ipfs-api in my application, I am getting below error:

ERROR TypeError: Buffer.from is not a function
    at varintEncode (vendor.js:185602)
    at Object../node_modules/is-ipfs/node_modules/multicodec/src/varint-table.js
    .....

My typescript file

var ipfsAPI = require('ipfs-api');
....
ngOnInit() {
   this.ipfsApi = ipfsAPI(this.globals.ipfsIp, '5001');
}

Reason

The files mentioned in the error statement use 'Buffer.from' internally.

Version Details

I read somewhere that it could be due to version issue and Buffer API is only available in Node v5.10.0+.

Buffer - 5.6.0 (latest)

Node - 10.17.0 

ipfs-api - 26.1.2

So I don't think version is the issue in my case.

What I tried

To the files throwing error, I added:

const Buffer = require('buffer').Buffer 

and the error moved on to next file, obviously this is not a solution and just a trial.

.

How to fix this issue? Any help would be appreciated.

like image 761
Noopur Tiwari Avatar asked May 26 '26 01:05

Noopur Tiwari


1 Answers

I added this

const { Buffer } = require("node:buffer");

it seemed to work because the variable is now wrapped in curly braces.

previously I had this

const Buffer = require("node:buffer");
like image 174
CHALK100 Avatar answered May 28 '26 13:05

CHALK100



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!