@yukiakai/resolve-package
v1.1.1
Published
Reliable way to find the root directory of a Node.js package, even in pnpm monorepos.
Maintainers
Readme
@yukiakai/resolve-package
🔍 Reliable way to get the root directory or metadata of a Node.js package — even with
pnpm!
@yukiakai/resolve-package helps you find the actual root directory of any installed package in your project (where its package.json is located), and even read that package.json — regardless of whether you use npm, yarn, or pnpm (including with symlinks and virtual stores).
🚀 Why?
Most existing solutions like require.resolve, pkg-dir, or resolve only give you the entry file path — not the actual root directory of the package.
In modern pnpm setups, they often fail or give incorrect results due to symlinks and virtual folders.
This library solves it by resolving the entry file, then walking up the directory tree to find the real package.json.
📦 Installation
npm install @yukiakai/resolve-package
# or
pnpm add @yukiakai/resolve-package
# or
yarn add @yukiakai/resolve-package🔧 Usage
ESM / TypeScript
import { resolvePath, resolveMetadata } from '@yukiakai/resolve-package';
const rootPath = resolvePath('lodash');
const meta = resolveMetadata('lodash');
console.log(rootPath);
// → /absolute/path/to/node_modules/.pnpm/[email protected]/node_modules/lodash
console.log(meta.name, meta.version);
// → lodash 4.17.21CommonJS
const { resolvePath, resolveMetadata } = require('@yukiakai/resolve-package');
console.log(resolvePath('express'));
console.log(resolveMetadata('express'));🧩 API
resolvePath(pkgName: string, basedir?: string): string | null
- Returns absolute path to the root folder of the given package (where its
package.jsonis). - Returns
nullif the package cannot be resolved.
resolveMetadata(pkgName: string, basedir?: string): object | null
- Returns parsed contents of the package's
package.json - Returns
nullif not found or invalid
✅ Features
- Works with pnpm, npm, and yarn
- Handles symlinks, hoisted, and virtual stores
- No assumptions about project layout
📂 Example output (with pnpm):
resolvePath('vite');
// → /home/user/project/node_modules/.pnpm/[email protected]/node_modules/vite
resolveMetadata('vite');
// → { name: 'vite', version: '5.1.0', ... }🧪 Run Tests
npm run testUses Vitest.
❓ FAQ
Q: Why not just use require.resolve()?
A: It only gives you the main file (some/folder/index.js, etc.), not the folder containing package.json. Also, it doesn't work correctly with ESM modules.
Q: Does it work with workspaces / monorepos?
A: Yes. It uses resolve under the hood, and always finds the correct installed location.
📦 Changelog
See full release notes in CHANGELOG.md
📄 License
MIT © 2025 Yuki Akai
