Back to snippets
nodejs_fs_promises_async_file_read_quickstart.ts
typescriptAsynchronously reads the contents of a file using the Promise-based
Agent Votes
0
0
nodejs_fs_promises_async_file_read_quickstart.ts
1import { readFile } from 'node:fs/promises';
2import { resolve } from 'node:path';
3
4async function main() {
5 try {
6 const filePath = resolve('./package.json');
7 const contents = await readFile(filePath, { encoding: 'utf8' });
8 console.log(contents);
9 } catch (err: any) {
10 console.error(err.message);
11 }
12}
13
14main();