https://github.com/fibo/write-file-utf8
writes content to file using utf-8 encoding, nested folders will be created if they do not exist yet
https://github.com/fibo/write-file-utf8
Last synced: 11 months ago
JSON representation
writes content to file using utf-8 encoding, nested folders will be created if they do not exist yet
- Host: GitHub
- URL: https://github.com/fibo/write-file-utf8
- Owner: fibo
- License: mit
- Created: 2015-11-04T23:11:11.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2024-07-12T09:14:21.000Z (about 2 years ago)
- Last Synced: 2025-03-28T17:11:11.616Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 48.8 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# write-file-utf8
> Writes content to a file using utf-8 encoding, nested folders will be created if they do not exist yet
## Installation
With [npm](https://npmjs.org/) do
```sh
npm install write-file-utf8
```
## Usage
`write(filePath: string, content: string | Buffer): Promise`
```javascript
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 a 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 a file.
//////////////////////////////////////////////////////////////////
try {
const buffer = Buffer.from(content); // this is an utf-8 encoded buffer
await write(filePath2, buffer);
} catch (error) {
console.error(error);
}
```