Noopur Tiwari
Noopur Tiwari

Reputation: 122

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.

Upvotes: 9

Views: 20179

Answers (2)

CHALK100
CHALK100

Reputation: 175

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");

Upvotes: 6

explorer
explorer

Reputation: 952

Looks like Buffer.from is added in Buffer v5.10.0 enter image description here.

Try to update Node.js version to v10.20.1. It has Buffer.from function.

Upvotes: 0

Related Questions