
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
parse-lock-files
Advanced tools
A comprehensive Node.js library for parsing lockfiles from npm, Yarn (v1, v2, v3, v4), and pnpm (v7, v8, v9, v10).
yaml for YAML parsing)| Package Manager | Versions | Lockfile Name |
|---|---|---|
| npm | v7+ | package-lock.json |
| Yarn Classic | v1.x | yarn.lock |
| Yarn Berry | v2.x, v3.x, v4.x | yarn.lock |
| pnpm | v7.x, v8.x, v9.x, v10.x | pnpm-lock.yaml |
npm install parse-lock-files
Find the lockfile in a directory without parsing it:
import { findLockfile } from 'parse-lock-files';
const lockfilePath = await findLockfile('./my-project');
// Returns: '/absolute/path/to/package-lock.json' (or yarn.lock, or pnpm-lock.yaml)
The function searches for lockfiles in this order:
package-lock.jsonyarn.lockpnpm-lock.yamlFind and parse the lockfile in one step:
import { findAndParseLockfile } from 'parse-lock-files';
const result = await findAndParseLockfile('./my-project');
console.log(result.type); // 'npm', 'yarn', or 'pnpm'
console.log(result.packages); // Parsed package information
If you already have the lockfile content, you can parse it directly:
import { parseLockfile } from 'parse-lock-files';
import { readFileSync } from 'fs';
const content = readFileSync('package-lock.json', 'utf-8');
const result = parseLockfile(content);
console.log(result.type); // 'npm', 'yarn', or 'pnpm'
console.log(result.packages); // Parsed package information
import { detectLockfileType } from 'parse-lock-files';
const type = detectLockfileType(content);
// Returns: 'npm', 'yarn-v1', 'yarn-berry', 'pnpm', or null
import {
parseNpmLockfile,
parseYarnV1Lockfile,
parseYarnBerryLockfile,
parsePnpmLockfile
} from 'parse-lock-files';
// Parse npm lockfile
const npmResult = parseNpmLockfile(npmContent);
// Parse Yarn v1 lockfile
const yarnV1Result = parseYarnV1Lockfile(yarnV1Content);
// Parse Yarn Berry (v2+) lockfile
const yarnBerryResult = parseYarnBerryLockfile(yarnBerryContent);
// Parse pnpm lockfile
const pnpmResult = parsePnpmLockfile(pnpmContent);
findLockfile(directory: string): Promise<string>Finds a lockfile in the given directory and returns its path. Searches for package-lock.json, yarn.lock, and pnpm-lock.yaml in that order.
Parameters:
directory: Path to the directory containing a lockfileReturns: Promise resolving to the absolute path of the found lockfile
Throws: Error if no lockfile is found in the directory
Example:
const lockfilePath = await findLockfile('./my-project');
// '/absolute/path/to/my-project/package-lock.json'
findAndParseLockfile(directory: string): Promise<object>Finds a lockfile in the given directory, automatically detects its type, and parses it. Internally uses findLockfile() to locate the file.
Parameters:
directory: Path to the directory containing a lockfileReturns: Promise resolving to:
type: 'npm', 'yarn', or 'pnpm'packages: Object containing parsed package informationThrows: Error if no lockfile is found in the directory
parseLockfile(content: string): objectAutomatically detects the lockfile type from content and parses it.
Parameters:
content: The lockfile content as a stringReturns:
type: 'npm', 'yarn', or 'pnpm'packages: Object containing parsed package informationdetectLockfileType(content: string): string | nullDetects the type of lockfile from its content.
Parameters:
content: The lockfile content as a stringReturns: 'npm', 'yarn-v1', 'yarn-berry', 'pnpm', or null
All parsers return packages in a normalized structure with the following core fields:
{
version: string, // Resolved version number
resolved: string, // Download URL or resolution string
integrity: string, // Integrity/checksum hash
dependencies: object, // Runtime dependencies
devDependencies: object, // Development dependencies
optionalDependencies: object, // Optional dependencies
peerDependencies: object, // Peer dependencies
engines: object, // Engine requirements
// ... plus format-specific fields
}
Each lockfile format may include additional format-specific fields:
license, bin, funding, cpu, oslanguageName, linkType, bin, conditionsdev, optional, hasBin, cpu, osparseNpmLockfile(content: string)Parses npm package-lock.json (lockfileVersion 3+).
Package Keys: "node_modules/package-name"
Returns:
{
type: 'npm',
lockfileVersion: number,
name: string,
version: string,
packages: { [key: string]: NormalizedPackage },
dependencies: object,
requires: object
}
parseYarnV1Lockfile(content: string)Parses Yarn v1 yarn.lock files.
Package Keys: "package@version-range" (e.g., "lodash@^4.0.0")
Returns:
{
type: 'yarn',
version: 1,
packages: { [key: string]: NormalizedPackage }
}
parseYarnBerryLockfile(content: string)Parses Yarn Berry (v2, v3, v4) yarn.lock files.
Package Keys: "package@npm:version-range" (e.g., "lodash@npm:^4.0.0")
Returns:
{
type: 'yarn',
version: number, // 2, 3, or 4
metadata: {
version: number,
cacheKey: string
},
packages: { [key: string]: NormalizedPackage }
}
parsePnpmLockfile(content: string)Parses pnpm pnpm-lock.yaml files (v7, v8, v9, v10).
Package Keys: "package@version" (e.g., "lodash@4.17.21")
Returns:
{
type: 'pnpm',
lockfileVersion: string,
settings: object,
importers: object, // v9+
dependencies: object,
devDependencies: object,
specifiers: object, // v7
packages: { [key: string]: NormalizedPackage },
snapshots: object // v9+ dependency trees
}
npm test
npm run test:watch
.
├── src/
│ ├── index.js # Main entry point with unified API
│ ├── npm.js # npm lockfile parser
│ ├── yarn-v1.js # Yarn v1 parser
│ ├── yarn-berry.js # Yarn Berry (v2+) parser
│ └── pnpm.js # pnpm parser
├── test/
│ ├── index.test.js
│ ├── npm.test.js
│ ├── yarn-v1.test.js
│ ├── yarn-berry.test.js
│ └── pnpm.test.js
└── fixtures/ # Test fixtures with real lockfiles
├── npm/
├── yarn-v1/
├── yarn-v2/
├── yarn-v3/
├── yarn-v4/
├── pnpm-v7/
├── pnpm-v8/
├── pnpm-v9/
└── pnpm/
Apache-2.0
Contributions are welcome! Please feel free to submit a Pull Request.
FAQs
Parse lockfiles from npm, yarn, and pnpm
We found that parse-lock-files demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.