node-yaml is a streamlined wrapper library for the popular js-yaml parser, simplifying file-based YAML operations in Node.js environments. It provides convenient asynchronous and synchronous methods like `read`, `write`, `readSync`, and `writeSync` for handling YAML files. The current stable version is 4.0.1. Released with native ESM support in v4.0.0, it shifted js-yaml to a peer dependency, requiring manual installation of js-yaml. It aims for a stable, active release cadence, indicated by recent minor updates following a major rewrite. Its key differentiator is abstracting away the boilerplate of file system operations when working with YAML data, making it easier to parse and serialize YAML to and from files compared to directly using js-yaml's `safeLoad` and `safeDump` functions with `fs` module operations.
npm install node-yamlVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to write a JavaScript object to a YAML file and then read it back using both explicit and implicit file extensions, utilizing `node-yaml`'s asynchronous API.
Run `npm install node-yaml js-yaml` or `yarn add node-yaml js-yaml`.
Migrate all `require()` statements to `import` statements and ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`).
Refactor code to use `async/await` or Promise chains (`.then().catch()`) for `read()` and `write()` calls.
Modify imports to fetch specific `js-yaml` utilities directly from the `js-yaml` package, e.g., `import { Type } from 'js-yaml';`.Always provide the full file path including the extension for clarity and to prevent ambiguity, especially when multiple files with similar names might exist.
Install `js-yaml` manually: `npm install js-yaml` or `yarn add js-yaml`.
Change `const { read } = require('node-yaml')` to `import { read } from 'node-yaml'` and ensure your project is configured for ESM.Update your code to correctly handle Promises, e.g., `const data = await read('file.yaml');` or `read('file.yaml').then(data => ...);`.