JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 710
  • Score
    100M100P100Q103706F
  • License MIT

writes content to file using utf-8 encoding, tries to create directory with mkdirp

Package Exports

  • write-file-utf8

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (write-file-utf8) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

write-file-utf8

writes content to file using utf-8 encoding, tries to create directory with mkdirp

Installation | API | Usage | See also | License

Installation

With npm do

npm install write-file-utf8

API

write(filePath, content)

It is an function that returns a Promise and requires the following parameters:

  • @param {String} filePath can be inside a nested folder that does not exist yet
  • @param {String|Buffer} content will be written using utf-8 encoding

Usage

const write = require('write-file-utf8')

// Nested folders will be created if they do not exist yet.
const filePath1 = '/tmp/foo/bar.txt'
const filePath2 = '/tmp/foo/bar/quz.txt'

const content = 'Hello'

// Since write-file-utf8 function will return a Promise,
// the most comfortable way to run it is inside an async function.
async function example () {
  try {
    await write(filePath1, content)

    const buffer = new Buffer.from(content) // this is an utf-8 encoded buffer
    await write(filePath2, buffer)
  } catch (error) {
    // In case you do not have permissions to create folders,
    // you may want to handle that case here.
    console.error(error)
  }
}

// Run example.
example()

See also

License

MIT