# write-to-file

> Writes data to file and automatically create its directories if not exists.

Latest version **1.0.2** (published 2018-12-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install write-to-file
pnpm add write-to-file
yarn add write-to-file
bun add write-to-file
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2018-12-07 |
| First published | 2018-11-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 1 |
| Unpacked size | 6.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Risan Bagja Pradana |
| Maintainers | risan |
| Keywords | fs, mkdirp, write-file |

## Links

- npm: https://www.npmjs.com/package/write-to-file
- Repository: https://github.com/risan/write-to-file
- Homepage: https://github.com/risan/write-to-file#readme
- Issues: https://github.com/risan/write-to-file/issues
- npm.io page: https://npm.io/package/write-to-file

## Dependencies (1)

- [create-dir](https://npm.io/package/create-dir.md) 1.x

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2018-12-07
- 1.0.1 — 2018-12-02
- 1.0.0 — 2018-11-28

## README

# Write to File

[![Build Status](https://badgen.net/travis/risan/write-to-file)](https://travis-ci.org/risan/write-to-file)
[![Test Covarage](https://badgen.net/codecov/c/github/risan/write-to-file)](https://codecov.io/gh/risan/write-to-file)
[![Greenkeeper](https://badges.greenkeeper.io/risan/write-to-file.svg)](https://greenkeeper.io)
[![Latest Version](https://badgen.net/npm/v/write-to-file)](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)

---
_Source: https://npm.io/package/write-to-file · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
