json2yaml: JSON to YAML Converter
This package, `json2yaml`, provides a command-line utility and a programmatic API for converting JSON data into human-readable YAML format. First released with a Node.js `0.2.0` engine requirement, its current stable version is 1.1.0. The tool primarily aims to pretty-print JSON, transforming it into the more visually appealing, whitespace-based YAML notation, leveraging the fact that JSON is technically a proper subset of YAML. This means any valid JSON is also valid YAML, but `json2yaml` focuses on converting to the more common, indented YAML style for improved human readability. The project appears to have ceased active development around 9 years ago, suggesting it is no longer actively maintained and may have compatibility issues with modern JavaScript environments.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ES Module environment or a browser without a CommonJS bundler.fixEnsure your Node.js project is running in a CommonJS context (e.g., `"type": "commonjs"` in `package.json` or explicitly using `.cjs` file extensions). For browsers, use a bundler like Webpack or Rollup that handles CommonJS modules. -
TypeError: json2yaml.stringify is not a function
cause Incorrectly importing or accessing the `stringify` method, or attempting to call the default export directly as a function.fixEnsure you are using `const json2yaml = require('json2yaml');` and then calling `json2yaml.stringify(data);`. The `stringify` method is a property of the module's default export. -
json2yaml: command not found
cause The `json2yaml` command-line utility is not in your system's PATH, usually because it was not installed globally or npm's global bin directory is not in PATH.fixInstall the package globally using `npm install -g json2yaml`. If the problem persists, ensure your npm global bin directory is included in your system's PATH environment variable (e.g., `~/.npm-global/bin`).
Warnings
- breaking The package specifies Node.js >= 0.2.0 and has not been updated in over 9 years. It is highly likely to have compatibility issues with modern Node.js versions (v14+), especially regarding ES Modules, deprecated APIs, and event loop behavior.
- gotcha This package is CommonJS-only. Attempting to use ES Modules `import` statements will result in `ReferenceError: require is not defined` or similar errors in pure ESM environments. You must use `require()` or configure your project for CJS interop.
- gotcha As an unmaintained package, `json2yaml` is unlikely to receive security patches for newly discovered vulnerabilities. Using it in production environments is not recommended without thorough security audits.
- gotcha The package does not ship with TypeScript declaration files (`.d.ts`). Usage in TypeScript projects will require manual type declarations or suppressing type checks for imports.
Install
-
npm install json2yaml -
yarn add json2yaml -
pnpm add json2yaml
Imports
- YAML
import YAML from 'json2yaml';
const YAML = require('json2yaml'); - stringify
import { stringify } from 'json2yaml';const { stringify } = require('json2yaml'); // or const YAML = require('json2yaml'); const ymlText = YAML.stringify(data); - json2yaml (CLI)
json2yaml <file.json> > <file.yml>
Quickstart
const json2yaml = require('json2yaml');
// Sample JSON data for conversion
const data = {
product: 'Widget X',
version: '1.0.0',
features: [
{ name: 'Feature A', enabled: true },
{ name: 'Feature B', enabled: false, note: 'Experimental' }
],
settings: {
debugMode: false,
logLevel: 'info'
},
nullValue: null,
price: 99.99,
available: true
};
try {
// Convert the JSON object to a YAML string
const yamlOutput = json2yaml.stringify(data);
console.log('--- Converted YAML Output ---\n');
console.log(yamlOutput);
} catch (error) {
console.error('An error occurred during JSON to YAML conversion:', error.message);
}