UnhandledPromiseRejectionWarning: ReferenceError: ReadableStream is not defined

2.5k views Asked by At

I'm trying to convert an arraybuffer of a file to a readable stream in Typescript but when i'm trying to create a new ReadableStream variable I obtain this error:

UnhandledPromiseRejectionWarning: ReferenceError: ReadableStream is not defined

What is the problem ? This is my code :

export function ReadableBufferStream(ab: ArrayBuffer) {
  return new ReadableStream({
    start(controller) {
      controller.enqueue(ab)
      controller.close()
    }
  })
}

export async function putFileToIPFS(file:ArrayBuffer): Promise<string>{
  const readableStream = ReadableBufferStream(file)
  let cid ;
  try {
  console.log("PRINT BEFORE PIN")
  cid = await pinata.pinFileToIPFS(readableStream)
  console.log(cid)
  }   
  catch (error) { console.error(error);}
  return cid['IpfsHash']
}

does anyone know how to help me? I start from a buffer array and would like to be able to get a ReadableStream to be able to load it on IPFS. A thousand thanks

2

There are 2 answers

0
Nalin Ranjan On

Can you try this(importing ReadableStream from node types)...

import { ReadableStream } from 'node:stream/web'; 


import { ReadableStream } from 'node:stream/web'; // <-- Put this in the beginning of the code file
// ....
// ....
// ....
export function ReadableBufferStream(ab: ArrayBuffer) {
  return new ReadableStream({
    start(controller) {
      controller.enqueue(ab)
      controller.close()
    }
  })
}

export async function putFileToIPFS(file:ArrayBuffer): Promise<string>{
  const readableStream = ReadableBufferStream(file)
  let cid ;
  try {
  console.log("PRINT BEFORE PIN")
  cid = await pinata.pinFileToIPFS(readableStream)
  console.log(cid)
  }   
  catch (error) { console.error(error);}
  return cid['IpfsHash']
}
0
Rakshith HR On

I encountered the same issue when using Node version 14. After researching, I found that upgrading Node version to 16.5 or higher should have resolved the problem but didn't. The reason was that I was using two versions of Node(14 & 18) using NVM, and the default Node version set by NVM was 14.

This meant that the globally installed packages (in my case it was pm2) were installed under Node version 14. This became evident when I examined the logs and saw the following error:

ReferenceError: ReadableStream is not defined
at Object.<anonymous> (/home/ubuntu/backend/node_modules/langchain/dist/util/stream.cjs:8:38)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Module._compile (/home/ubuntu/backend/node_modules/pirates/lib/index.js:136:24)
at Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Object.newLoader [as .cjs] (/home/ubuntu/backend/node_modules/pirates/lib/index.js:141:7)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Module.require (internal/modules/cjs/loader.js:974:19)
at Module.Hook._require.Module.require (/home/ubuntu/.nvm/versions/node/v14.19.0/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)

To address this issue, I followed these steps:

  1. I changed the default Node version in NVM to 18 using the below command

    nvm alias default 18.17.0

  2. I uninstalled the global packages associated with both Node versions 14 and 18.

  3. I reinstalled the packages, ensuring they were installed under Node 18 and the issue got fixed.

Please ensure you are using Node version 16.5 or higher and that your packages are installed under the appropriate Node version.