node package manager

write-file-utf8

write-file-utf8

shortcut to fs.writeFile

Installation | API | Usage | See also | License

Installation

With npm do

npm install write-file-utf8

API

writeFileUtf8(filePath, content[, callback])

  • @param {String} filePath
  • @param {String} content
  • @param {Function} [callback] defaults to a trivial if (err) throw err

writeFileUtf8.error

An object exposing the following error messages:

  • contentIsNotString

For example, try the following snippet

var write = require('write-file-utf8')
 
try {
  var buffer = new Buffer('a')
  write('/tmp/foo', buffer)
} catch (err) {
  if (err.message === write.error.contentIsNotString) {
    console.log('Hey, are buffers utf-8 encoded?')
  }
}

Usage

var write = require('write-file-utf8')
 
var filePath = '/tmp/foo'
var content = 'bar'
 
write(filePath, content)

Actually is the same as

var fs = require('fs')
 
var filePath = '/tmp/foo'
var content = 'bar'
 
function throwError (err) {
  if (err) {
    throw err
  }
}
 
fs.writeFile(filePath, content, 'utf8', throwError)

It accepts also an optional callback, for example

write(filePath, content, (err) => {
  if (err) throw err
 
  console.log(`File saved: ${filePath}`)
})

See also

License

MIT