# yaml-loader

> YAML loader for Webpack

Latest version **0.9.0** (published 2025-12-28) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 45/100 (D)** — status: stable.

Positive: no vulnerabilities.

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

## Facts

| | |
|---|---|
| Version | 0.9.0 |
| Published | 2025-12-28 |
| First published | 2014-06-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 20 |
| Dependencies | 2 |
| Unpacked size | 6.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 153 |
| Maintainers | okonet, eemeli |
| Keywords | yaml, webpack, loader, json |

## Links

- npm: https://www.npmjs.com/package/yaml-loader
- Repository: https://github.com/eemeli/yaml-loader
- Issues: https://github.com/eemeli/yaml-loader/issues
- npm.io page: https://npm.io/package/yaml-loader

## Dependencies (2)

- [yaml](https://npm.io/package/yaml.md) ^2.0.0
- [javascript-stringify](https://npm.io/package/javascript-stringify.md) ^2.0.1

## 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.9.0 (latest) — 2025-12-28
- 0.8.1 — 2024-02-21
- 0.8.0 — 2022-04-26
- 0.7.0 — 2022-04-09
- 0.6.0 — 2020-03-21
- 0.5.0 — 2017-07-03
- 0.4.0 — 2016-07-22
- 0.3.0 — 2016-07-14
- 0.2.0 — 2016-05-12
- 0.1.0 — 2014-06-13

## README

# yaml-loader for Webpack

YAML loader for [Webpack](https://webpack.js.org/). Allows importing YAML files as JS objects. Uses [`yaml`](https://www.npmjs.com/package/yaml) internally.

## Installation

```sh
npm install --save-dev yaml-loader
```

## Usage

```js
// webpack.config.js
module.exports = {
  module: {
    rules: [
      {
        test: /\.ya?ml$/,
        use: 'yaml-loader'
      }
    ]
  }
}
```

```yaml
# file.yaml
---
config:
  js:
    key: test
hello: world
```

```js
// application.js
import file from './file.yaml'

file.hello === 'world'
```

## Options

In addition to all [`yaml` options](https://eemeli.org/yaml/#options) used by its parsing methods,
the loader supports the following additional options:

### `asJSON`

If enabled, the loader output is stringified JSON rather than stringified JavaScript.
Also useful for chaining with other loaders that expect JSON input.

### `asStream`

If enabled, parses the source file as a stream of YAML documents. With this, the output will always be an array, with entries for each document. If set, `namespace` is ignored.

To use this option for only some YAML files, it's probably easiest to use a query parameter and match that using [Rule.resourceQuery](https://webpack.js.org/configuration/module/#ruleresourcequery):

```js
// webpack.config.js
module.exports = {
  module: {
    rules: [
      {
        test: /\.ya?ml$/,
        oneOf: [
          {
            resourceQuery: /stream/,
            options: { asStream: true },
            loader: 'yaml-loader'
          },
          { loader: 'yaml-loader' }
        ]
      }
    ]
  }
}
```

Then, importing `./foo.yaml` will expect it to contain only one document, but `./bar.yaml?stream` may contain multiple documents.

### `namespace`

Allows for exposing a sub-tree of the source document:

```js
import jsCfg from './file.yaml?namespace=config.js'

jsCfg.key === 'test'
```

The `namespace` should be a series of keys, dot separated. Note that any `options` object in your `webpack.config.js` rule will be superseded by a `?query`.

## License

[MIT](http://www.opensource.org/licenses/mit-license.php)

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