How to Read a File in Bucklescript

Trying to read in a file into Bucklescript?

Perhaps, you have a json file that needs to be loaded?

Here’s how you would do that in Bucklescript:

Node.Fs.readFileAsUtf8Sync "filename"

Here’s the documentation on readFileAsUtf8Sync:

And if you need to read in some Json:

let json =
  |> Node.Fs.readFileAsUtf8Sync 
  |> Js.Json.parseExn

Here’s the docs for parseExn:

That’s it!

If you need to further decode the json, stay tuned for a future article on using “bs-json” library for that.