0

I am trying to convert my image to base64. So I create this function for this. But the problem is when I try to run my program I have an error called ReferenceError: FileReader is not defined. I have no idea what is this error about.

This is my code..?

const toDataURL = url => fetch(url)
  .then(response => response.blob())
  .then(blob => new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.onloadend = () => resolve(reader.result)
    reader.onerror = reject
    reader.readAsDataURL(blob)
  }).catch((error) => {
     console.log(error);
  });
)


toDataURL('https://www.gravatar.com/avatar/d50c83cc0c6523b4d3f6085295c953e0')
  .then(dataUrl => {
    console.log('RESULT:', dataUrl)
  })
2
  • 2
    It is because FileReader is part of web API not of javascript so it is not available in nodejs instead of this you can use the fs module available in nodejs you can read about fs on official nodejs docs Commented Aug 23, 2022 at 12:18
  • Does this help? stackoverflow.com/q/54099802/4225384 Commented Aug 23, 2022 at 13:11

1 Answer 1

2

FileReader is part of Web API in a browser but not in Node.js Try to use native Node.js method to read from a file or try to search an appropriate npm package

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.