# hjsonfile

> Easily read/write HJSON files.

Latest version **0.0.6** (published 2018-03-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install hjsonfile
pnpm add hjsonfile
yarn add hjsonfile
bun add hjsonfile
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2018-03-05 |
| First published | 2017-04-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 48.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | othree |
| Maintainers | othree |
| Keywords | read, write, file, json, hjson, fs, fs-extra |

## Links

- npm: https://www.npmjs.com/package/hjsonfile
- Repository: https://github.com/othree/node-hjsonfile
- Homepage: https://github.com/othree/node-hjsonfile#readme
- Issues: https://github.com/othree/node-hjsonfile/issues
- npm.io page: https://npm.io/package/hjsonfile

## Dependencies (2)

- [hjson](https://npm.io/package/hjson.md) ^3.1.1
- [graceful-fs](https://npm.io/package/graceful-fs.md) ^4.1.6

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.6 (latest) — 2018-03-05
- 0.0.5 — 2017-04-19
- 0.0.4 — 2017-04-19
- 0.0.3 — 2017-04-18
- 0.0.2 — 2017-04-05
- 0.0.1 — 2017-04-05

## README

Node.js - hjsonfile
================

Easily read/write HJSON files. Based on [jsonfile](https://www.npmjs.com/package/jsonfile).  

<a href="https://github.com/feross/standard"><img src="https://cdn.rawgit.com/feross/standard/master/sticker.svg" alt="Standard JavaScript" width="100"></a>


Why?
----

Writing `JSON.stringify()` and then `fs.writeFile()` and `JSON.parse()` with `fs.readFile()` enclosed in `try/catch` blocks became annoying.



Installation
------------

    npm install --save hjsonfile



API
---

### readFile(filename, [options], callback)

`options` (`object`, default `undefined`): Pass in any `fs.readFile` options and to [hjson.parse](https://www.npmjs.com/package/hjson#hjsonparsetext-options).
  - `throws` (`boolean`, default: `true`). If `JSON.parse` throws an error, pass this error to the callback.
  If `false`, returns `null` for the object.


```js
var jsonfile = require('hjsonfile')
var file = '/tmp/data.json'
jsonfile.readFile(file, function(err, obj) {
  console.dir(obj)
})
```


### readFileSync(filename, [options])

`options` (`object`, default `undefined`): Pass in any `fs.readFileSync` options and to [hjson.parse](https://www.npmjs.com/package/hjson#hjsonparsetext-options).

- `throws` (`boolean`, default: `true`). If `JSON.parse` throws an error, throw the error.
If `false`, returns `null` for the object.

```js
var jsonfile = require('hjsonfile')
var file = '/tmp/data.json'

console.dir(jsonfile.readFileSync(file))
```


### writeFile(filename, obj, [options], callback)

`options`: Pass in any `fs.writeFile` options and to [hjson.stringify](https://www.npmjs.com/package/hjson#hjsonstringifyvalue-options).


```js
var jsonfile = require('hjsonfile')

var file = '/tmp/data.json'
var obj = {name: 'JP'}

jsonfile.writeFile(file, obj, function (err) {
  console.error(err)
})
```

**formatting with space:**

```js
var jsonfile = require('hjsonfile')

var file = '/tmp/data.json'
var obj = {name: 'JP'}

jsonfile.writeFile(file, obj, {space: 2}, function(err) {
  console.error(err)
})
```

**appending to an existing JSON file:**

You can use `fs.writeFile` option `{flag: 'a'}` to achieve this.

```js
var jsonfile = require('hjsonfile')

var file = '/tmp/mayAlreadyExistedData.json'
var obj = {name: 'JP'}

jsonfile.writeFile(file, obj, {flag: 'a'}, function (err) {
  console.error(err)
})
```

### writeFileSync(filename, obj, [options])

`options`: Pass in any `fs.writeFileSync` options and to [hjson.stringify](https://www.npmjs.com/package/hjson#hjsonstringifyvalue-options).

```js
var jsonfile = require('hjsonfile')

var file = '/tmp/data.json'
var obj = {name: 'JP'}

jsonfile.writeFileSync(file, obj)
```

**formatting with space:**

```js
var jsonfile = require('hjsonfile')

var file = '/tmp/data.json'
var obj = {name: 'JP'}

jsonfile.writeFileSync(file, obj, {space: 2})
```

**appending to an existing JSON file:**

You can use `fs.writeFileSync` option `{flag: 'a'}` to achieve this.

```js
var jsonfile = require('hjsonfile')

var file = '/tmp/mayAlreadyExistedData.json'
var obj = {name: 'JP'}

jsonfile.writeFileSync(file, obj, {flag: 'a'})
```

### space

Global configuration to set space to indent JSON files.

**default:** `null`

```js
var jsonfile = require('hjsonfile')

jsonfile.space = 4

var file = '/tmp/data.json'
var obj = {name: 'JP'}

// json file has four space indenting now
jsonfile.writeFile(file, obj, function (err) {
  console.error(err)
})
```

Note, it's bound to `this.space`. So, if you do this:

```js
var myObj = {}
myObj.writeJsonSync = jsonfile.writeFileSync
// => this.space = null
```

Could do the following:

```js
var jsonfile = require('hjsonfile')
jsonfile.space = 4
jsonfile.writeFileSync(file, obj) // will have 4 spaces indentation

var myCrazyObj = {space: 32}
myCrazyObj.writeJsonSync = jsonfile.writeFileSync
myCrazyObj.writeJsonSync(file, obj) // will have 32 space indentation
myCrazyObj.writeJsonSync(file, obj, {space: 2}) // will have only 2
```

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