Registry / devops / zod-package-json

zod-package-json

JSON →
library2.1.0jsnpmunverified

A Zod v4 schema for validating full package.json files. Version 2.1.0 is latest, actively maintained, and requires Node >=20 and Zod ^4.0.0 as a peer dependency. It covers all npm-documented properties plus TypeScript- and Node-specific fields, preserves unknown properties, and exports both classic Zod and Zod mini schemas. Compared to ad‑hoc schemas, it is comprehensive, typed, and kept current with npm's docs.

npm install zod-package-json
INSTALL
IMPORT
SIG · ZOD-PACKAGE-JSON
Z
zod-package-json
devopsjavascriptv2.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

PackageJson
import { PackageJson } from 'zod-package-json'
const PackageJson = require('zod-package-json')
ESM-only since v2. CommonJS require() will fail with ERR_REQUIRE_ESM.
PackageJson (mini schema)
import { PackageJson } from 'zod-package-json/mini'
import { PackageJson } from 'zod-package-json' // this gives the classic Zod schema, not mini
Use the /mini subpath import for the smaller Zod mini version. Types are built-in.
PackageJsonType (inferred type)
import { PackageJson } from 'zod-package-json'; type Pkg = typeof PackageJson._type;
import { PackageJsonType } from 'zod-package-json'
The package does not export an explicit type alias for the inferred type. Use Zod's _type property.
z (Zod object used internally)
// Not exported directly; import zod separately
import { z } from 'zod-package-json'
This package does not re-export Zod. Always import zod separately: import { z } from 'zod'.

Parse a package.json file, access known and unknown properties, and handle parse errors with safeParse.

import { PackageJson } from 'zod-package-json'; import { readFileSync } from 'fs'; const raw = JSON.parse(readFileSync('./package.json', 'utf-8')); const pkg = PackageJson.parse(raw); console.log(pkg.name, pkg.version); // Access unknown properties via string index const extra = pkg['myCustomField']; // unknown preserved console.log(extra); // Parse failure example const result = PackageJson.safeParse({ version: 'not-semver' }); if (!result.success) { console.error(result.error.issues); }
Debug
Known issues
breakingVersion 2.x is ESM-only. require() will throw ERR_REQUIRE_ESM.
fix
Use dynamic import() or switch to ESM (e.g., "type": "module" in your package.json).
affects: >=2.0.0
breakingPeer dependency Zod v4 required; Zod v3 not supported.
fix
Install zod@^4.0.0 alongside this package.
affects: >=2.0.0
gotchaThe default export is not a function; must use named import { PackageJson }.
fix
Use import { PackageJson } from 'zod-package-json' (or '/mini').
affects: *
gotchaThe /mini subpath import is separate from the main schema. Mixing them will not work.
fix
Always import from a single entry point; do not mix import from root and from /mini in the same module.
affects: *
deprecatedVersion 1.x used Zod v3 and CommonJS. Version 2.x is a major rewrite.
fix
Upgrade to v2 and ensure Zod v4 is installed. See CHANGELOG for migration details.
affects: <2.0.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module not supported.
Using CommonJS require() with ESM-only v2 package.
fix
Convert to dynamic import: const { PackageJson } = await import('zod-package-json');
Cannot find module 'zod-package-json'?
Package not installed or incorrect import path.
fix
Run npm install zod-package-json and ensure import is correct: import { PackageJson } from 'zod-package-json';
The inferred type of 'PackageJson' cannot be referenced directly.
No explicit type export; uses Zod _type.
fix
Use typeof PackageJson._type to get the inferred type.
TypeError: (0 , zod_package_json_1.PackageJson) is not a function
Using default import instead of named import.
fix
Use import { PackageJson } from 'zod-package-json';
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies
zodrequiredPeer dependency – Zod v4 is required at runtime for schema validation.
Agent activity
29 hits · last 30 days
node
24
Amazon
2
Resources
zod-package-json — npm install zod-package-json · libregistry