{"id":11180,"library":"json2yaml","title":"json2yaml: JSON to YAML Converter","description":"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.","status":"abandoned","version":"1.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/coolaj86/json2yaml","tags":["javascript","yml","yaml","json","cli","util"],"install":[{"cmd":"npm install json2yaml","lang":"bash","label":"npm"},{"cmd":"yarn add json2yaml","lang":"bash","label":"yarn"},{"cmd":"pnpm add json2yaml","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import YAML from 'json2yaml';","symbol":"YAML","correct":"const YAML = require('json2yaml');"},{"note":"The primary function for programmatic conversion is exposed as 'stringify' on the default export. Direct named import using ES Module syntax is not supported.","wrong":"import { stringify } from 'json2yaml';","symbol":"stringify","correct":"const { stringify } = require('json2yaml');\n// or\nconst YAML = require('json2yaml');\nconst ymlText = YAML.stringify(data);"},{"note":"The package is primarily designed for global command-line interface (CLI) use after `npm install -g json2yaml`.","symbol":"json2yaml (CLI)","correct":"json2yaml <file.json> > <file.yml>"}],"quickstart":{"code":"const json2yaml = require('json2yaml');\n\n// Sample JSON data for conversion\nconst data = {\n  product: 'Widget X',\n  version: '1.0.0',\n  features: [\n    { name: 'Feature A', enabled: true },\n    { name: 'Feature B', enabled: false, note: 'Experimental' }\n  ],\n  settings: {\n    debugMode: false,\n    logLevel: 'info'\n  },\n  nullValue: null,\n  price: 99.99,\n  available: true\n};\n\ntry {\n  // Convert the JSON object to a YAML string\n  const yamlOutput = json2yaml.stringify(data);\n  console.log('--- Converted YAML Output ---\\n');\n  console.log(yamlOutput);\n} catch (error) {\n  console.error('An error occurred during JSON to YAML conversion:', error.message);\n}","lang":"javascript","description":"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."},"warnings":[{"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.","message":"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.","severity":"breaking","affected_versions":"all"},{"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.","message":"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.","severity":"gotcha","affected_versions":"all"},{"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.","message":"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.","severity":"gotcha","affected_versions":"all"},{"fix":"Create a declaration file (e.g., `json2yaml.d.ts`) with `declare module 'json2yaml' { ... }` or use `@ts-ignore` on the import statement.","message":"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.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"Attempting to use `require()` in an ES Module environment or a browser without a CommonJS bundler.","error":"ReferenceError: require is not defined"},{"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.","cause":"Incorrectly importing or accessing the `stringify` method, or attempting to call the default export directly as a function.","error":"TypeError: json2yaml.stringify is not a function"},{"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`).","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.","error":"json2yaml: command not found"}],"ecosystem":"npm"}