write-file-utf8
TypeScript icon, indicating that this package has built-in type declarations

3.0.2 • Public • Published

write-file-utf8

writes content to file using utf-8 encoding, nested folders will be created if they do not exist yet

Installation | API | Usage | See also | License

Installation

With npm do

npm install write-file-utf8

API

write(filePath: string, content: string | Buffer): Promise<void>

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

import write from "write-file-utf8";

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

const content = "Hello";

// Write a `string` into file.
//////////////////////////////////////////////////////////////////
try {
  await write(filePath1, content);
} catch (error) {
  // In case you do not have permissions to create folders,
  // you may want to handle it here.
  console.error(error);
}

// Can also write a `Buffer` into file.
//////////////////////////////////////////////////////////////////
try {
  const buffer = Buffer.from(content); // this is an utf-8 encoded buffer
  await write(filePath2, buffer);
} catch (error) {
  console.error(error);
}

See also

License

MIT

Package Sidebar

Install

npm i write-file-utf8

Weekly Downloads

985

Version

3.0.2

License

MIT

Unpacked Size

6.2 kB

Total Files

5

Last publish

Collaborators

  • fibo