Unhandled Rejection (TypeError): fs.createReadStream is not a function. Error at the time of image uploading to ipfs( pinata)

Viewed 414

I was trying to upload image to pinata ipfs cloud from my frontend app of react js. But it shows me error:Unhandled Rejection (TypeError): fs.createReadStream is not a function. .

Here is my code snippet:

require('dotenv').config();
const fs = require('fs');

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

const key = process.env.REACT_APP_PINATA_KEY;
const secret = process.env.REACT_APP_PINATA_SECRET;

export const pinFileToIPFS = async (filePath) => {
const url = "https://api.pinata.cloud/pinning/pinFileToIPFS";
//we gather a local file for this example, but any valid readStream source will work here.
let data = new FormData();
data.append('file',fs.createReadStream(filePath));

return axios.post(url,
    data,
    {
        maxContentLength: 'Infinity', //this is needed to prevent axios from erroring out with large files
        headers: {
            'Content-Type': `multipart/form-data; boundary=${data._boundary}`,
            'pinata_api_key': key,
            'pinata_secret_api_key': secret
        }
  })
  .then(function (response) {
      //handle response here
      return {
        success: true,
        pinataUrl: "https://gateway.pinata.cloud/ipfs/" + response.data.IpfsHash
    };
  })
  .catch(function (error) {
      //handle error here
      console.log(error)
        return {
            success: false,
            message: error.message,
        }
  });
};

How to fix it Or Is there any way of doing this rather than using 'fs'?

1 Answers
  1. 'fs' package has been deemed as malicious software, from github security placeholder page: "This package contained malicious code and was removed from the registry by the npm security team...". I wouldn't recommend using it.
  2. The error message is specifying (TypeError): fs.createReadStream is not a function.
  3. While doing research, I found fs-extra npm package is a good replacement for fs.

With fs-extra, you can use the readJson (one of many methods) method executing it with a callback, promises, or async/await e.g.:

const fse = require('fs-extra');

...

try {
    const fileObject = await fse.readJson(filePath);
    data.append('file', fileObject);
    console.log(fileObject);
} catch (err) {
    console.error(err);
}

...

If this is doesn't work, based on this node.js example attempt the following:

  1. Create a variable with the createReadStream method:
 // This line opens the file as a readable stream
let readStream = fs.createReadStream(filePath);
  1. Use the following as intended to retrieve the response and catch any errors
  // This will wait until we know the readable stream is actually valid before piping
  readStream.on('open', function () {
    // This just pipes the read stream to the response object (which goes to the client)
    readStream.pipe(res);
    // in your case:
    data.append('file', res);
  });

  // This catches any errors that happen while creating the readable stream (usually invalid names)
  readStream.on('error', function(err) {
    res.end(err);
  });
Related