7

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.

Noopur Tiwari
  • 102
  • 1
  • 1
  • 7

2 Answers2

4

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");
CHALK100
  • 135
  • 1
  • 7
0

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.

explorer
  • 944
  • 8
  • 18
  • I updated the version to 10.20.1, but no success. Same error. – Noopur Tiwari May 25 '20 at 13:56
  • 1
    I think, you need to update `ipfs-api` as well. It is deprecated https://www.npmjs.com/package/ipfs-api. Its npm page advices to use https://www.npmjs.com/package/ipfs-http-client. I hope you wouldn't face any compatibility issues. – explorer May 26 '20 at 02:28