Categories
JavaScript Answers

How to fix JSON.parse(fs.readFileSync()) returning a buffer with Node?

Spread the love

To fix JSON.parse(fs.readFileSync()) returning a buffer with Node, we specify the encoding.

For instance, we write

const squadJSON = JSON.parse(fs.readFileSync("file.json", "utf8"));

to call readFileSync to open the file.json file.

We specify the encoding is 'utf8' to return a Unicode string instead of a buffer.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *