reads content from file using utf-8 encoding, also imports JSON files easily
With npm do
npm install read-file-utf8read<T = string>(filePath: string): Promise<T>
Read from a text file.
import read from 'read-file-utf8';
const filePath = 'path/to/file.txt';
try {
// Read file content.
const content = await read(filePath)
console.log(content)
} catch (error) {
// In case you do not have permissions,
// you may want to handle it here.
console.error(error)
}Read from a JSON file: given the .json extension then JSON.parse is used to parse it.
// Read version from package.json file.
const { version } = await read('./package.json');
console.log(version)If you are using TypeScript can provide the type of your JSON.
const { version } = await read<{ version: string }>('./package.json');Warning
No validation is done on the JSON content.