To use the readSync function from fs-extra in Node.js, follow the steps below:
21 chars2 lines
index.tsx32 chars2 lines
index.tsx127 chars5 lines
In the code above, we read the contents of a file located at path/to/your/file.txt
in utf-8
encoding. The contents of the file are stored in the fileContents
variable, which we then log to the console.
Note that the readFileSync
function is synchronous, which means it blocks the event loop until the file has been read. If you need to read a large file, it's better to use the readFile
function, which is asynchronous and non-blocking.
gistlibby LogSnag