JSON to Pretty YAML Converter
This Node.js module provides a straightforward utility for converting JSON data into human-readable, 'pretty' YAML format. The package is currently at version 1.2.2, which was last updated over six years ago as of early 2026. It functions as a lightweight wrapper around the `js-yaml` library, specifically utilizing its stringification capabilities to produce well-formatted YAML output. Due to its age and lack of recent updates, users should be aware of potential compatibility issues with newer Node.js versions (e.g., ESM compatibility) or security concerns related to its underlying dependencies, which may not have received recent patches. The module's primary differentiator is its simplicity and direct focus on producing aesthetically pleasing YAML from JSON inputs without complex configuration options.
Common errors
-
TypeError: (0 , json_to_pretty_yaml_1.stringify) is not a function
cause Attempting to use ES module `import { stringify } from 'json-to-pretty-yaml';` in an ES module context.fixIf in an ES module environment, you must dynamically import: `const YAML = await import('json-to-pretty-yaml');` and then `YAML.default.stringify(...)`. In a CommonJS environment, use `const YAML = require('json-to-pretty-yaml');`. -
Cannot find module 'json-to-pretty-yaml'
cause The package is not installed or the import path is incorrect.fixEnsure the package is installed: `npm install json-to-pretty-yaml`. Verify the module name is correctly spelled in your `require()` statement.
Warnings
- gotcha This package is CommonJS-only (`require`) and does not natively support ES Modules (`import`). Attempting to `import` it will result in errors.
- breaking The package has not been updated in over six years and is considered abandoned. This means no new features, bug fixes, or security patches will be released.
- gotcha The package depends on `js-yaml@^3.9.0`, which is an old version of the `js-yaml` library. This may expose projects to known vulnerabilities or compatibility issues present in older `js-yaml` releases.
- gotcha Due to its age, this package might exhibit compatibility issues with very recent Node.js runtime versions or modern JavaScript language features (e.g., async/await within its own codebase, though its API is synchronous).
Install
-
npm install json-to-pretty-yaml -
yarn add json-to-pretty-yaml -
pnpm add json-to-pretty-yaml
Imports
- YAML
import YAML from 'json-to-pretty-yaml';
const YAML = require('json-to-pretty-yaml'); - stringify
import { stringify } from 'json-to-pretty-yaml';const { stringify } = require('json-to-pretty-yaml');
Quickstart
const fs = require('fs');
const path = require('path');
const YAML = require('json-to-pretty-yaml');
// Create a dummy input.json file for demonstration
const inputJsonPath = path.join(__dirname, 'input.json');
const outputYamlPath = path.join(__dirname, 'output.yaml');
const inputJsonContent = {
"project": "MyAwesomeProject",
"version": "1.0.0",
"config": {
"env": "development",
"features": [
"featureA",
"featureB"
]
},
"settings": null,
"enabled": true
};
fs.writeFileSync(inputJsonPath, JSON.stringify(inputJsonContent, null, 2));
// Load the JSON data (or use an object directly)
const json = require(inputJsonPath);
// Convert JSON to YAML
const yamlData = YAML.stringify(json);
// Write the YAML to a file
fs.writeFileSync(outputYamlPath, yamlData);
console.log(`Converted JSON to YAML:
${yamlData}`);
console.log(`Output written to ${outputYamlPath}`);
// Clean up dummy file
fs.unlinkSync(inputJsonPath);
fs.unlinkSync(outputYamlPath);