# yaml-reader

> A lightweight wrapper for js-yaml to easily read yml files

Latest version **1.0.4** (published 2023-06-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install yaml-reader
pnpm add yaml-reader
yarn add yaml-reader
bun add yaml-reader
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2023-06-13 |
| First published | 2018-05-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 25.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Alex Sabau |
| Maintainers | asab-se |
| Keywords | nodejs, yaml |

## Links

- npm: https://www.npmjs.com/package/yaml-reader
- Repository: https://github.com/asab-se/yaml-reader
- Homepage: https://github.com/asab-se/yaml-reader#readme
- Issues: https://github.com/asab-se/yaml-reader/issues
- npm.io page: https://npm.io/package/yaml-reader

## Dependencies (1)

- [js-yaml](https://npm.io/package/js-yaml.md) ^3.13.*

## Alternatives

- [monaco-yaml](https://npm.io/package/monaco-yaml.md) — 420.1K weekly downloads
- [@crewx/workflow](https://npm.io/package/@crewx/workflow.md) — 3.1K weekly downloads
- [yaml-cat](https://npm.io/package/yaml-cat.md) — 38 weekly downloads
- [nunjucks-in-yaml](https://npm.io/package/nunjucks-in-yaml.md) — 9 weekly downloads
- [shopify-symlinks](https://npm.io/package/shopify-symlinks.md) — 3 weekly downloads

## Recent versions

- 1.0.4 (latest) — 2023-06-13
- 1.0.3 — 2019-07-23
- 1.0.2 — 2019-07-23
- 1.0.1 — 2019-07-23
- 1.0.0 — 2019-07-19
- 0.4.0 — 2018-05-25
- 0.3.1 — 2018-05-11
- 0.3.0 — 2018-05-11
- 0.2.1 — 2018-05-11
- 0.2.0 — 2018-05-11
- 0.1.0 — 2018-05-10

## README

# YAML Reader

This is a lightweight reader of yaml files. It's only purpose is to read
yaml files to use its attributes in a node app. Therefore it keeps simple
and its dependencies low.

The asynchronous read provides promise based and callback based support, thus 
you can choose the style you like better.

## Notes

yaml-reader is written in ECMA 6 (Node version 8.11.1). Be sure your node version can handle
this or use babel for older node versions. No guarantee that everything works fine when using babel.

## Installation

```
npm install --save yaml-reader
```

<br/>

## Usage

yaml-reader resolves the path **from your project root onwards**, i.e. lets assume the project structure given below.
Then you will call `yamlReader.read('configs/app-config.yml')` wherever you want to read the yaml file. Thus the call
 will look the same in both `bin/app.js` and `some/deep/project/path/someService.js`.   
```
+-- package.json
+-- README.md
+-- bin
+---- app.js
+-- some
+---- deep
+------ project
+-------- path
+---------- someService.js
+---- ...
+-- configs
+---- app-config.yml
```

<br/>

#### Synchronous read

Read a yaml file synchronously. This is like requiring a json config file via `require()`:

```
const config = require('yaml-reader').read('path/from/project/root/to/yml.yml');
```

<br/>

#### Asynchronous read

You can also read a yaml asynchronously. The result will be returned as a `Promise` or, if
a callback function is provided, with a callback.

##### Promise based:
```
const yamlReader = require('yaml-reader');

yamlReader.readAsync('path/from/project/root/to/yml.yml')
.then((config) => {
    ...
})
.catch((err) => {
    ...
})
```

##### Callback based:
```
const yamlReader = require('yaml-reader');

// without options, thus null as 2nd arg
yamlReader.readAsync('path/from/project/root/to/yml.yml', null, (err, config) => {
    if (err) {
        ...
    }
    else {
        ...
    }
})
```

In all cases you can pass in the encoding of the file with an option object. If this is not passed, *utf8* will be used by default.

```
const options = {
    encoding: yamlReader.constants.ENCODING.UTF_16_LE
};
const config = require('yaml-reader').read('path/from/project/root/to/yml.yml', options);

// or async

yamlReader.readAsync('path/from/project/root/yml.yml', options, (err, config) => {
    ...
}

yamlReader.readAsync('path/from/project/root/yml.yml', options)
.then((config) => {
    ...
})
```

<br/>

## API

#### yamlReader.read(file [, options])
#### yamlReader.readYaml(file [, options])

Read a yaml file _synchronously_ .

- __file__ _\<string>_ : The path to the yaml file to read. Resolves from project root onwards. Read "Usage" section
 to see how it is resolved if unclear
- __options__ _<Object | null>_ : Options parsed to the reader
    - __options.encoding__ _<string | null>_: The encoding of the yaml file. Is 'utf-8' by default.
    You can use `yaml-reader.constants.ENCODING` for supported encodings.

```
const config = require('yaml-reader').read('path/from/project/root/to/yml.yml');
```

Or with encoding specified:

```
const options = {
    encoding: yamlReader.constants.ENCODING.UTF_8
};
const config = require('yaml-reader').read('path/from/project/root/to/yml.yml', options);
```

<hr/>

#### yamlReader.readAsync(file [, options, callback])
#### yamlReader.readYamlAsync(file [, options, callback])

Read a yaml file _asynchronously_. The yaml attributes will be returned with a Promise.

- __file__ _\<string>_ : The path to the yaml file to read. Resolves from project root onwards. Read "Usage" section 
to see how it is resolved if unclear
- __options__ _<Object | null>_ : Options parsed to the reader
    - __options.encoding__ _<string | null>_: The encoding of the yaml file. Is 'utf-8' by default.
    You can use `yaml-reader.constants.ENCODING` for supported encodings.
- __callback__ _<function | null>_ : An optional callback. If this is null the reader assumes you use Promises instead.
    So take care to use the promise syntax if you do not provide a callback.

The config is either returned as a _Promise\<Object>.resolve_ if the file was readable or rejected otherwise, or with
the callback if invoked with a callback.

```
const yamlReader = require('yaml-reader');
const options = { ... }
//
// awaiting promises
//

yamlReader.readAsync('path/from/project/root/to/yml.yml', options)
.then((config) => {
    // access properties of your yaml
    // console.log(config.myProperty)
})
.catch((err) => {
    // handle errors (like FileNotFoundError)
})

//
// or with callback
//

yamlReader.readAsync('path/from/project/root/to/yml.yml', options, (err, config) => {
    ...
}
```

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