Registry / serialization / json2yaml

json2yaml

JSON →
library1.2.0jsnpmunverified

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.

npm install json2yaml
INSTALL
IMPORT
SIG · JSON2YAML
J
json2yaml
serializationjavascriptv1.2.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

YAML
const YAML = require('json2yaml');
import YAML from 'json2yaml';
This package is CommonJS-only and exposes its API via `module.exports`. It does not support ES Modules `import` syntax, which will lead to runtime errors in environments not configured for CJS interop.
stringify
const { stringify } = require('json2yaml'); // or const YAML = require('json2yaml'); const ymlText = YAML.stringify(data);
import { stringify } from 'json2yaml';
The primary function for programmatic conversion is exposed as 'stringify' on the default export. Direct named import using ES Module syntax is not supported.
json2yaml (CLI)
json2yaml <file.json> > <file.yml>
The package is primarily designed for global command-line interface (CLI) use after `npm install -g json2yaml`.

This code snippet demonstrates how to use `json2yaml` programmatically to convert a JavaScript object (which translates to JSON) into a human-readable YAML string using the `stringify` method.

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); }
json2yaml --version
Debug
Known issues
breakingThe 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.
fix
Consider using actively maintained alternatives like `js-yaml` or `yaml` for modern Node.js environments. If you must use `json2yaml`, run it in a legacy Node.js environment or a container configured with older Node.js versions.
affects: all
gotchaThis 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.
fix
Ensure your project is configured for CommonJS (e.g., `"type": "commonjs"` in `package.json`) or use dynamic `import()` if you are in an ESM context and need to load a CJS module.
affects: all
gotchaAs 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.
fix
Prioritize using actively maintained libraries for security-sensitive applications. If this package is critical, consider auditing its source code or forking it for maintenance.
affects: all
gotchaThe 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.
fix
Create a declaration file (e.g., `json2yaml.d.ts`) with `declare module 'json2yaml' { ... }` or use `@ts-ignore` on the import statement.
affects: all
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to use `require()` in an ES Module environment or a browser without a CommonJS bundler.
fix
Ensure 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
Incorrectly importing or accessing the `stringify` method, or attempting to call the default export directly as a function.
fix
Ensure 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
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.
fix
Install 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`).
Upgrade
Version history
1.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources