Reading json files in NodeJS: require() vs fs.readFile()
Often we save settings or configurations in
If you've spent some time using NodeJS, you'd know that there are quite a few
ways to read json files in NodeJS
Two of the most common ones are : https://gist.github.com/c9536d535c31c4c07ae0d4192086f97b
The other way is using the
We can either do it synchronously https://gist.github.com/243832e0a1246cba12f7d6b7c1ae843f
Or we can do it asynchronously -
Now obviously the question that comes to mind is which method to use, and if there are any obvious benefits to any one method
Let us discuss the differences -
Caching : (If the file data changes)
require() will cache the file in the require graph.
So during the lifetime of the node app, if the
file.json is changed,
you will not get the new data, even if you re-run
On the other hand
fs.readFileSync will always
re-read the file, and pick up changes.
Encoding : (UTF-8 is used 99% times, but still...)
require you cannot define the file encoding. 99% of the times, that is
not a problem.
Nevertheless if your json is not encoded in Unicode/UTF-8, you'd have to use
fs.readFile as it supports encodings such as ascii or latin1 or even
base64 (yes, yes, I hear you, no one saves JSON as base64)
Sync vs Async
require() is synchronous, and hence blocking in nature.
fs provides both sync and async methods as shown above. If you want to
read your JSON file without blocking, then
fs.readFile is your only option.
NOTE: From NodeJS 10.x,
import file from './file.json' would be possible
because of support for
modules, which would allow async reading of json files
I hope you'll be able to take an informed decision in your future projects based on the points discussed here.