read-file-utf8
reads content from file using utf-8 encoding
API | Usage | Examples | See also | License
API
readFile(filePath: String): Promise<String>
It is a function that returns a Promise and requires one String parameter:
- @param
{String}
filePath - @returns
{Promise<String>}
content
Usage
Read from a text file.
const readFile = require('read-file-utf8')
const filePath = 'file.txt'
// Since read-file-utf8 function will return a Promise,
// the most comfortable way to run it is inside an async function.
async function example () {
try {
// Read file content.
////////////////////
const content = await readFile(filePath)
console.log(content)
} catch (error) {
// In case you do not have read permissions,
// you may want to handle it here.
console.error(error)
}
}
// Run example.
example()
Examples
Import JSON
This makes sense at the time of this writing (2020) since it is not possibile to import JSON using require
when ES modules are enabled in Node.
For example to read version from a package.json you can do something like the following.
async function showPackageJsonVersion () {
const { version } = await readFile('package.json').then(JSON.parse)
console.log(version)
}
showPackageJsonVersion()
Read SQL files
Suppose you have some SQL queries. It is really better to put every query in its own queryFile.sql good old SQL file, instead then inside someOtherFile.js JavaScript file.
Create a sql/ folder and put there all your queries. Add also a sql/index.js with the following content
const path = require('path')
const readFile = require('read-file-utf8')
function sql (fileName) {
return readFile(path.join(__dirname, `${fileName}.sql`))
}
module.exports = sql
Suppose there is a sql/count_winners.sql file with the following content
SELECT COUNT(*) AS num
FROM foo.contest
WHERE is_winner IS TRUE
Now you are able to do, for example
const pg = require('pg')
const sql = require('./path/to/sql/index.js')
const connectionString = '@@@your connection string here@@@'
pg.connect(connectionString, async function (err, client, done) {
if (err) return console.error(err)
const sqlCode = await sql('count_winners')
client.query(sqlCode, function (err, result) {
if (err) return console.error(err)
console.log(result.rows[0].num)
})
})