Skip to content

Instantly share code, notes, and snippets.

@kwokhou
Created August 29, 2022 00:08
Show Gist options
  • Save kwokhou/9eca515ab07d848bbcfd525c1c91ad3b to your computer and use it in GitHub Desktop.
Save kwokhou/9eca515ab07d848bbcfd525c1c91ad3b to your computer and use it in GitHub Desktop.
Reading and writing to file in NodeJS
const fs = require('fs');
const readFile = async filePath => {
try {
const data = await fs.promises.readFile(filePath, 'utf8')
return data
}
catch(err) {
console.log(err)
}
}
const writeFile = async (filePath, fileContent) => {
try {
await fs.promises.writeFile(filePath, fileContent)
}
catch(err) {
console.log(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment