Read a JSON file in Node

Two ways, one of them without any fs code at all.

SNP 038 #416 Filed js node

The classic way:

javascript
import { readFile } from 'node:fs/promises'

const config = JSON.parse(await readFile('./config.json', 'utf8'))

Or import it like a module. Node 22 and newer need the attribute:

javascript
import config from './config.json' with { type: 'json' }

The import is cached, so if the file changes while the program runs you won't see it. Use readFile for anything that gets edited at runtime.