Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.
Node.js built-in modules famously are not promise-based.
This is for historical reasons, as those modules were created before promises were a thing.
promisify has been available for a long time; Node.js also provides a promise-based fs API.
This API was added in Node.js 10 (2018) and is available only for the fs module.
Here is how to use it:
import * as fs from 'node:fs/promises';
Note: the
node:fsprefix identifies Node.js built-in modules.
Now you can use any of the fs methods using promises or await:
const posts = await fs.readdir('content')