{"name":"pkg-types","version":"2.3.1","description":"Node.js utilities and TypeScript definitions for `package.json` and `tsconfig.json`","license":"MIT","repository":{"type":"git","url":"git+https://github.com/unjs/pkg-types.git"},"sideEffects":false,"main":"./dist/index.mjs","types":"./dist/index.d.mts","exports":{".":"./dist/index.mjs"},"dependencies":{"confbox":"^0.2.4","exsolve":"^1.0.8","pathe":"^2.0.3"},"devDependencies":{"@types/node":"^25.2.3","@typescript/native-preview":"latest","@vitest/coverage-v8":"^4.0.18","automd":"^0.4.3","changelogen":"^0.6.2","eslint-config-unjs":"^0.6.2","expect-type":"^1.3.0","obuild":"^0.4.27","oxfmt":"^0.32.0","oxlint":"^1.47.0","prettier":"^3.8.1","typescript":"^5.9.3","vitest":"^4.0.18"},"scripts":{"build":"obuild","dev":"vitest --typecheck","lint":"oxlint && oxfmt --check .","fmt":"automd && oxlint --fix . && oxfmt","release":"pnpm test && pnpm build && changelogen --release && npm publish && git push --follow-tags","test":"pnpm lint && pnpm typecheck","typecheck":"tsgo --noEmit --skipLibCheck"},"_id":"pkg-types@2.3.1","bugs":{"url":"https://github.com/unjs/pkg-types/issues"},"homepage":"https://github.com/unjs/pkg-types#readme","_integrity":"sha512-y+ichcgc2LrADuhLNAx8DFjVfgz91pRxfZdI3UDhxHvcVEZsenLO+7XaU5vOp0u/7V/wZ+plyuQxtrDlZJ+yeg==","_resolved":"/private/var/folders/5g/r5lg700163d_r_6t_547flw80000gn/T/6817f967e90241ad0fa33417604aa95e/pkg-types-2.3.1.tgz","_from":"file:pkg-types-2.3.1.tgz","_nodeVersion":"24.15.0","_npmVersion":"11.12.1","dist":{"integrity":"sha512-y+ichcgc2LrADuhLNAx8DFjVfgz91pRxfZdI3UDhxHvcVEZsenLO+7XaU5vOp0u/7V/wZ+plyuQxtrDlZJ+yeg==","shasum":"fa27ed0940efcf40bba453b0e5cab41217b0d442","tarball":"http://registry-npm.ywwl.com/pkg-types/download/pkg-types-2.3.1.tgz","fileCount":5,"unpackedSize":46562,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIFdXsycpsLgmaXMEd8e3M8yPvma18wOkSxnA9EJ/iyfeAiBrE9ziLoUtag1Dzdjbb5zGfhApPfDEP/nZCtzclKxaNw=="}],"size":13059,"noattachment":false,"key":"/pkg-types/-/pkg-types-2.3.1.tgz"},"_npmUser":{"name":"danielroe","email":"daniel@roe.dev"},"directories":{},"maintainers":[{"name":"pi0","email":"pyapar@gmail.com"},{"name":"danielroe","email":"daniel@roe.dev"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/pkg-types_2.3.1_1777285455131_0.6312122790330033"},"_hasShrinkwrap":false,"readme":"# pkg-types\n\n<!-- automd:badges color=yellow codecov -->\n\n[![npm version](https://img.shields.io/npm/v/pkg-types?color=yellow)](https://npmjs.com/package/pkg-types)\n[![npm downloads](https://img.shields.io/npm/dm/pkg-types?color=yellow)](https://npm.chart.dev/pkg-types)\n[![codecov](https://img.shields.io/codecov/c/gh/unjs/pkg-types?color=yellow)](https://codecov.io/gh/unjs/pkg-types)\n\n<!-- /automd -->\n\nNode.js utilities and TypeScript definitions for `package.json`, `tsconfig.json`, and other configuration files.\n\n## Install\n\n<!-- automd:pm-i -->\n\n```sh\n# ✨ Auto-detect\nnpx nypm install pkg-types\n\n# npm\nnpm install pkg-types\n\n# yarn\nyarn add pkg-types\n\n# pnpm\npnpm add pkg-types\n\n# bun\nbun install pkg-types\n\n# deno\ndeno install npm:pkg-types\n```\n\n<!-- /automd -->\n\n## Usage\n\n### Package Configuration\n\n#### `readPackage`\n\nReads any package file format (package.json, package.json5, or package.yaml) with automatic format detection.\n\n```js\nimport { readPackage } from \"pkg-types\";\nconst localPackage = await readPackage();\n// or\nconst pkg = await readPackage(\"/fully/resolved/path/to/folder\");\n```\n\n#### `writePackage`\n\nWrites package data with format detection based on file extension.\n\n```js\nimport { writePackage } from \"pkg-types\";\n\nawait writePackage(\"path/to/package.json\", pkg);\nawait writePackage(\"path/to/package.json5\", pkg);\nawait writePackage(\"path/to/package.yaml\", pkg);\n```\n\n#### `findPackage`\n\nFinds the nearest package file (package.json, package.json5, or package.yaml).\n\n```js\nimport { findPackage } from \"pkg-types\";\nconst filename = await findPackage();\n// or\nconst filename = await findPackage(\"/fully/resolved/path/to/folder\");\n```\n\n#### `readPackageJSON`\n\n```js\nimport { readPackageJSON } from \"pkg-types\";\nconst localPackageJson = await readPackageJSON();\n// or\nconst packageJson = await readPackageJSON(\"/fully/resolved/path/to/folder\");\n```\n\n#### `writePackageJSON`\n\n```js\nimport { writePackageJSON } from \"pkg-types\";\n\nawait writePackageJSON(\"path/to/package.json\", pkg);\n```\n\n#### `resolvePackageJSON`\n\n```js\nimport { resolvePackageJSON } from \"pkg-types\";\nconst filename = await resolvePackageJSON();\n// or\nconst packageJson = await resolvePackageJSON(\"/fully/resolved/path/to/folder\");\n```\n\n#### `updatePackage`\n\nReads a package file and passes a proxied PackageJson to a callback (the callback may mutate it in-place or return a new object). The updated package is then written back using the same file format (`.json`/`.json5`/`.yaml`). The proxy auto-creates common map fields (e.g. `scripts`, `dependencies`) when accessed.\n\n```js\nimport { updatePackage } from \"pkg-types\";\n\nawait updatePackage(\"path/to/package\", (pkg) => {\n  pkg.version = \"1.0.1\";\n  pkg.dependencies.lodash = \"^4.17.21\";\n});\n```\n\n#### `sortPackage`\n\nReturns a new PackageJson that reorders known top-level fields according to the convention and alphabetically sorts certain nested maps (like `dependencies`, `devDependencies`, `optionalDependencies`, `peerDependencies` and `scripts`). Unknown top-level keys retain their original relative order. The input object is not mutated.\n\n```js\nimport { sortPackage } from \"pkg-types\";\n\nconst sorted = sortPackage(pkg);\n```\n\n#### `normalizePackage`\n\nNormalizes a `PackageJson` for stable output: sorts top-level fields and dependency maps, and removes dependency fields (`dependencies`, `devDependencies`, `optionalDependencies`, `peerDependencies`) if they are not plain objects. Returns a new normalized object.\n\n```js\nimport { normalizePackage } from \"pkg-types\";\n\nconst normalized = normalizePackage(pkg);\n```\n\n### TypeScript Configuration\n\n#### `readTSConfig`\n\n```js\nimport { readTSConfig } from \"pkg-types\";\nconst tsconfig = await readTSConfig();\n// or\nconst tsconfig2 = await readTSConfig(\"/fully/resolved/path/to/folder\");\n```\n\n#### `writeTSConfig`\n\n```js\nimport { writeTSConfig } from \"pkg-types\";\n\nawait writeTSConfig(\"path/to/tsconfig.json\", tsconfig);\n```\n\n#### `resolveTSConfig`\n\n```js\nimport { resolveTSConfig } from \"pkg-types\";\nconst filename = await resolveTSConfig();\n// or\nconst tsconfig = await resolveTSConfig(\"/fully/resolved/path/to/folder\");\n```\n\n### File Resolution\n\n#### `findFile`\n\n```js\nimport { findFile } from \"pkg-types\";\nconst filename = await findFile(\"README.md\", {\n  startingFrom: id,\n  rootPattern: /^node_modules$/,\n  test: (filename) => filename.endsWith(\".md\"),\n});\n```\n\n#### `findNearestFile`\n\n```js\nimport { findNearestFile } from \"pkg-types\";\nconst filename = await findNearestFile(\"package.json\");\n```\n\n#### `findFarthestFile`\n\n```js\nimport { findFarthestFile } from \"pkg-types\";\nconst filename = await findFarthestFile(\"package.json\");\n```\n\n#### `resolveLockfile`\n\nFind path to the lock file (`yarn.lock`, `package-lock.json`, `pnpm-lock.yaml`, `npm-shrinkwrap.json`, `bun.lockb`, `bun.lock`, `deno.lock`) or throws an error.\n\n```js\nimport { resolveLockfile } from \"pkg-types\";\nconst lockfile = await resolveLockfile(\".\");\n```\n\n#### `findWorkspaceDir`\n\nTry to detect workspace dir by in order:\n\n1. Farthest workspace file (`pnpm-workspace.yaml`, `lerna.json`, `turbo.json`, `rush.json`, `deno.json`, `deno.jsonc`)\n2. Closest `.git/config` file\n3. Farthest lockfile\n4. Farthest `package.json` file\n\nIf fails, throws an error.\n\n```js\nimport { findWorkspaceDir } from \"pkg-types\";\nconst workspaceDir = await findWorkspaceDir(\".\");\n```\n\n### Git Configuration\n\n#### `resolveGitConfig`\n\nFinds closest `.git/config` file.\n\n```js\nimport { resolveGitConfig } from \"pkg-types\";\n\nconst gitConfig = await resolveGitConfig(\".\");\n```\n\n#### `readGitConfig`\n\nFinds and reads closest `.git/config` file into a JS object.\n\n```js\nimport { readGitConfig } from \"pkg-types\";\n\nconst gitConfigObj = await readGitConfig(\".\");\n```\n\n#### `writeGitConfig`\n\nStringifies git config object into INI text format and writes it to a file.\n\n```js\nimport { writeGitConfig } from \"pkg-types\";\n\nawait writeGitConfig(\".git/config\", gitConfigObj);\n```\n\n#### `parseGitConfig`\n\nParses a git config file in INI text format into a JavaScript object.\n\n```js\nimport { parseGitConfig } from \"pkg-types\";\n\nconst gitConfigObj = parseGitConfig(gitConfigINI);\n```\n\n#### `stringifyGitConfig`\n\nStringifies a git config object into a git config file INI text format.\n\n```js\nimport { stringifyGitConfig } from \"pkg-types\";\n\nconst gitConfigINI = stringifyGitConfig(gitConfigObj);\n```\n\n## Types\n\n- **Note:** In order to make types work, you need to install `typescript` as a devDependency.\n\nYou can directly use typed interfaces:\n\n```ts\nimport type { TSConfig, PackageJson, GitConfig } from \"pkg-types\";\n```\n\n### Define Utilities\n\nYou can use define utilities for type support and auto-completion when working in plain `.js` files. These functions simply return the input object but provide TypeScript type hints.\n\n#### `definePackageJSON`\n\nProvides type safety and auto-completion for package.json objects.\n\n```js\nimport { definePackageJSON } from \"pkg-types\";\n\nconst pkg = definePackageJSON({\n  name: \"my-package\",\n  version: \"1.0.0\",\n  // TypeScript will provide auto-completion here\n});\n```\n\n#### `defineTSConfig`\n\nProvides type safety and auto-completion for tsconfig.json objects.\n\n```js\nimport { defineTSConfig } from \"pkg-types\";\n\nconst tsconfig = defineTSConfig({\n  compilerOptions: {\n    target: \"ES2020\",\n    // TypeScript will provide auto-completion here\n  },\n});\n```\n\n#### `defineGitConfig`\n\nProvides type safety and auto-completion for git config objects.\n\n```js\nimport { defineGitConfig } from \"pkg-types\";\n\nconst gitConfig = defineGitConfig({\n  user: {\n    name: \"John Doe\",\n    email: \"john@example.com\",\n  },\n  // TypeScript will provide auto-completion here\n});\n```\n\n## Alternatives\n\n- [dominikg/tsconfck](https://github.com/dominikg/tsconfck)\n\n## License\n\n<!-- automd:contributors license=MIT author=\"pi0,danielroe\" -->\n\nPublished under the [MIT](https://github.com/unjs/pkg-types/blob/main/LICENSE) license.\nMade by [@pi0](https://github.com/pi0), [@danielroe](https://github.com/danielroe) and [community](https://github.com/unjs/pkg-types/graphs/contributors) 💛\n<br><br>\n<a href=\"https://github.com/unjs/pkg-types/graphs/contributors\">\n<img src=\"https://contrib.rocks/image?repo=unjs/pkg-types\" />\n</a>\n\n<!-- /automd -->\n","publish_time":1777285455497,"_cnpm_publish_time":1777285455497,"dist-tags":{"latest":"2.4.0"}}