Skip to content

Instantly share code, notes, and snippets.

@championswimmer
Created December 5, 2017 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save championswimmer/f1467d422bcb8c348bb6677ccd890280 to your computer and use it in GitHub Desktop.
Save championswimmer/f1467d422bcb8c348bb6677ccd890280 to your computer and use it in GitHub Desktop.

Reading json files in NodeJS: require() vs fs.readFile()

Often we save settings or configurations in json files. 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 fs module.

We can either do it synchronously https://gist.github.com/243832e0a1246cba12f7d6b7c1ae843f

Or we can do it asynchronously -

https://gist.github.com/8d40b176603cfde343f9a47a68229303

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 require('./file.json')

On the other hand fs.readFile or fs.readFileSync will always re-read the file, and pick up changes.

Encoding : (UTF-8 is used 99% times, but still...)

In 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 without fs


I hope you'll be able to take an informed decision in your future projects based on the points discussed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment