https://github.com/risan/write-to-file
Writes data to file and automatically create its directories if not exists.
https://github.com/risan/write-to-file
fs mkdirp write-file
Last synced: 4 months ago
JSON representation
Writes data to file and automatically create its directories if not exists.
- Host: GitHub
- URL: https://github.com/risan/write-to-file
- Owner: risan
- License: mit
- Created: 2018-11-28T20:11:19.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-12-07T05:46:00.000Z (over 7 years ago)
- Last Synced: 2025-09-19T21:47:54.608Z (9 months ago)
- Topics: fs, mkdirp, write-file
- Language: JavaScript
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Write to File
[](https://travis-ci.org/risan/write-to-file)
[](https://codecov.io/gh/risan/write-to-file)
[](https://greenkeeper.io)
[](https://www.npmjs.com/package/write-to-file)
Writes data to file and automatically create its directories if not exists.
## Installation
```bash
$ npm install write-to-file
```
## Usage
```js
const writeToFile = require("write-to-file");
(async () => {
try {
await writeToFile("foo/bar/baz.txt", "Hello World!");
} catch(error) {
console.error(error.message);
}
})();
```
If `foo/bar` directory does not exist, it will be created automatically.
## Recipes
### Set the Character Encoding
You can pass the character encoding as the third argument. Default to `utf8`.
```js
const writeToFile = require("write-to-file");
(async () => {
const buff = Buffer.from("Hello World!");
try {
await writeToFile("foo.txt", buff.toString("hex"), "hex");
} catch(error) {
console.error(error.message);
}
})();
```
You can also pass an object:
```js
writeToFile("foo.txt", buff.toString("hex"), {
encoding: "hex"
});
```
### Appending Data to a File
By default, if the file already exists, it will be overwritten. For appending data to a file, you may pass the `flag` option:
```js
const writeToFile = require("write-to-file");
(async () => {
try {
await writeToFile("foo.txt", "bar", {
flag: "a"
});
} catch(error) {
console.error(error.message);
}
})();
```
## API
```js
writeToFile(file, data, [options])
```
### Parameters
* `file` (`String`): The file path to write to.
* `data` (`String`|`Buffer`): Data to write.
* `options` (Optional `Object`|`String`): You may pass the encoding as the third argument. You may also pass an object:
* `encoding` (`String`): The character encoding to use, default to `utf8`.
* `mode` (`Number`): The file permission to set, default to `0o666`.
* `flag` (`String`): The [file system](https://nodejs.org/dist/latest-v11.x/docs/api/fs.html#fs_file_system_flags) flag, default to `w`.
* `createDirMode` (`Number`): The directory permission to set when creating the parent directory that does not exist, default to `0o777`.
### Returns
It returns a `Promise` which when resolved contains a `true` value.
## Related
* [create-dir](https://github.com/risan/create-dir): Module to create directory recursively.
## License
[MIT](https://github.com/risan/write-to-file/blob/master/LICENSE) © [Risan Bagja Pradana](https://bagja.net)