npm ERR! Cannot read properties of null (reading 'matches')
Problem
The error npm ERR! Cannot read properties of null (reading 'matches') occurs during package installation with npm, often accompanied by dependency-related warnings. Key characteristics of this issue:
- Typically appears after running
npm installin a project directory - Frequently occurs when attempting to use npm in a project previously managed by pnpm
- Often includes peer dependency warnings in the error output
- Common with Node.js v18+ and npm v9+
- Persists even after clearing npm cache or changing registry mirrors
Example error output:
npm ERR! Cannot read properties of null (reading 'matches')
npm ERR! A complete log of this run can be found in:
npm ERR! /path/to/logs/debug.logSolutions
Primary Fix: Remove node_modules and Reinstall
The most effective solution is to remove the existing dependency folders and reinstall using a consistent package manager:
# Remove both node_modules and lock files
rm -rf node_modules
rm -f package-lock.json
rm -f pnpm-lock.yaml
# Then reinstall using either:
npm install # For npm projects
# OR
pnpm install # For pnpm projectsEnsure Package Manager Consistency
IMPORTANT
Never mix npm and pnpm commands in the same project
Stick to one package manager per project
Check for lock files to determine which manager your project uses:package-lock.json→ Usenpm installpnpm-lock.yaml→ Usepnpm install
bash# Check for lock files ls -la | grep -E 'package-lock|pnpm-lock'Enforce package manager in package.json
Add thepackageManagerfield to prevent accidental use of wrong commands:json{ "name": "your-project", "packageManager": "pnpm@9.7.1" // Specify your version }After adding this, tools like
create-next-appwill automatically use the specified package manager.
Alternative Solutions
Network issues
If you suspect network problems (common with certain firewalls):bash# Try changing networks npm install --fetch-retries=5 --fetch-retry-mintimeout=20000 # Or use mirrors (for China) npm config set registry https://registry.npmmirror.comClean reinstallation with pnpm
If switching to pnpm:bash# Remove existing files rm -rf node_modules package-lock.json # Generate fresh pnpm installation pnpm installVerify engine compatibility
Ensure your Node.js version matches project requirements inpackage.json:json"engines": { "node": ">=18.0.0" }
Prevention Tips
AVOID
Running npm install in a project that contains pnpm-lock.yaml
- Use
nvmto manage Node.js versions across projects - Configure VS Code to use project-specific package managers
- Add package manager hint to your project README:markdown
## Installation This project uses pnpm: ```bash pnpm install
Conclusion
The "Cannot read properties of null" error typically stems from package manager conflict between npm and pnpm. The guaranteed solution requires:
- Complete removal of
node_modulesand lock files - Consistent reinstallation using either npm or pnpm
- Declaration of
packageManagerin package.json for enforcement
This approach resolves the core conflict that triggers the "reading 'matches'" type error during dependency resolution.