To read and write JSON files in Node.js, use readFile and writeFile from node:fs/promises together with JSON.parse and JSON.stringify. Reading gives you a string that you parse into an object. Writing means turning the object back into a string and saving it. This is the pattern behind most "read write json file nodejs" tasks such as config files, small data stores and fixtures.
The examples target Node.js 22 or 24 LTS and use ES modules (import), which is what a new project should use.
Create a file named config.json next to your script:
Then read and parse it:
readFile returns a Buffer unless you pass an encoding. Passing 'utf8' gives you a string, which is what JSON.parse needs. Top-level await works in ES modules, so set "type": "module" in package.json or name the file .mjs.
Writing is the reverse. JSON.stringify converts a value to a JSON string, and writeFile saves it:
The second and third arguments of JSON.stringify are a replacer and an indentation. Passing null, 2 pretty-prints with two spaces, which makes the file readable and gives clean diffs in version control. Leave them out for the smallest output, for example when the file is machine-only.
writeFile replaces the whole file. If the file does not exist it is created, and if it exists its previous contents are lost.
A common task is read, change, write back:
The ENOENT check treats a missing file as an empty starting state and rethrows every other error. This read-modify-write cycle is not safe when two processes or two overlapping calls run at once, because one can overwrite the other. For real concurrent data use a database instead of a JSON file.
Two different things can fail, and they throw different errors:
| Failure | Error | How to detect it |
|---|---|---|
| File missing | system error | err.code === 'ENOENT' |
| No permission | system error | err.code === 'EACCES' |
| Path is a directory | system error | err.code === 'EISDIR' |
| Invalid JSON text | SyntaxError | err instanceof SyntaxError |
Adding the file path to the message saves time later, because the plain SyntaxError message does not mention which file was bad.
For static files that ship with your code, you can import JSON instead of reading it at runtime. In ES modules this needs an import attribute:
The module is loaded once and cached, so later edits to the file are not picked up until the process restarts. In CommonJS, require('./config.json') behaves the same way. Use readFile when the file can change while the program runs or when you need to write it back.
If the process crashes in the middle of writeFile, you can end up with a half-written, invalid JSON file. For files you cannot lose, write to a temporary file and rename it, because a rename on the same filesystem replaces the target in one step:
Keep in mind that JSON.stringify drops undefined values and functions, turns Date objects into ISO strings, and throws on circular references and on BigInt. Convert those yourself before saving.
Use the promise version inside servers and anything that handles many requests, because the sync version blocks the event loop. readFileSync is fine in a startup script that loads a config once.
The text usually starts with a byte order mark, an HTML error page, or is empty. Print the first characters of the string to see what you actually read.
No. Standard JSON has no comments, and JSON.parse throws on them. Use a different format such as JSON5 or YAML if you need comments, or keep notes in a separate field.
readFile loads the whole file into memory. For files too large for that, use a streaming format such as newline-delimited JSON and read it line by line with readline.
Read with readFile(path, 'utf8') and JSON.parse, and write with JSON.stringify(value, null, 2) and writeFile. Separate file errors (check err.code) from bad JSON (SyntaxError), use import attributes for static files, and write to a temporary file then rename when a half-written file would be costly.