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: https://bucklescript.github.io/bucklescript/api/Node.Fs.html


And if you need to read in some Json:

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

Here’s the docs for parseExn: https://bucklescript.github.io/bucklescript/api/Js.Json.html


That’s it!

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

Comments