# NPM Install Failed in CI
Common Error Patterns
npm install failures typically show:
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/packagenpm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency treenpm ERR! code ENOENT
npm ERR! syscall spawn git
npm ERR! errno ENOENTnpm ERR! network timeout
npm ERR! fetch failednpm ERR! peer dep missing: package@version, required by another-packageRoot Causes and Solutions
1. Registry Authentication Failure
Cannot access private registry or packages.
Solution:
Configure npm registry:
```bash npm config set registry https://registry.npmjs.org/
# For private registry (npm Enterprise, Artifactory) npm config set registry https://your-registry.com/ ```
Authenticate with private registry:
```bash npm login --registry=https://your-registry.com/
# Or use auth token npm config set //your-registry.com/:_authToken ${NPM_TOKEN} ```
In CI:
```yaml # GitHub Actions - name: Configure npm env: NPM_TOKEN: ${{ secrets.NPM_TOKEN }} run: | npm config set //registry.npmjs.org/:_authToken ${NPM_TOKEN} npm install
# Or use .npmrc file - name: Setup .npmrc run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > .npmrc
- name: Install dependencies
- run: npm ci
`
2. Dependency Resolution Conflict
Conflicting peer dependencies or version requirements.
Solution:
Use legacy peer deps resolution:
npm install --legacy-peer-depsOr force resolution:
npm install --forceBetter approach - resolve conflicts properly:
```bash # Check dependency tree npm ls package-name
# Audit for vulnerabilities npm audit fix
# Update lockfile npm update ```
In CI:
- name: Install dependencies
run: npm ci --legacy-peer-deps3. Package Not Found
Package doesn't exist or is private.
Solution:
Verify package exists:
npm view package-name
npm search package-nameCheck registry:
npm config get registry
curl -s https://registry.npmjs.org/package-nameFor scoped packages, ensure correct scope:
```bash # Check @scope/package npm view @scope/package
# Configure scope registry npm config set @scope:registry https://scope-registry.com/ ```
4. Lockfile Out of Sync
package-lock.json doesn't match package.json.
Solution:
Regenerate lockfile:
rm package-lock.json
npm installOr update lockfile:
npm install --lockfile-version 2Use npm ci for CI environments:
# npm ci uses lockfile exactly, faster and safer
npm ciIn CI:
- name: Install dependencies
run: npm ci # Uses lockfile, faster than npm install5. Network/Timeout Issues
Slow or unreliable network connection.
Solution:
Increase timeout:
npm install --fetch-timeout=60000
npm config set fetch-timeout 60000
npm config set fetch-retry-maxtimeout 120000Use mirror registry:
```bash # Use npm mirror npm config set registry https://registry.npmmirror.com/
# Switch back to default npm config set registry https://registry.npmjs.org/ ```
In CI:
- name: Install dependencies
run: npm ci --fetch-timeout=60000
timeout-minutes: 106. Git Dependencies Fail
Cannot clone git-based dependencies.
Solution:
Ensure git is available:
git --versionFor private git repos:
```yaml # GitHub Actions - name: Setup git credentials run: | git config --global url."https://${{ secrets.GITHUB_TOKEN }}@github.com".insteadOf "https://github.com"
- name: Install dependencies
- run: npm ci
`
For SSH git URLs:
# Configure SSH
ssh-keyscan github.com >> ~/.ssh/known_hosts7. Cache Corruption
npm cache causing installation issues.
Solution:
Clear npm cache:
npm cache clean --force
rm -rf node_modules
npm installIn CI:
```yaml - name: Clear npm cache run: npm cache clean --force
- name: Install dependencies
- run: npm ci
`
8. Node Version Mismatch
Node version incompatible with packages.
Solution:
Check Node version:
node --version
npm --versionUse correct Node version:
```yaml # GitHub Actions - name: Setup Node uses: actions/setup-node@v4 with: node-version: '18' cache: 'npm'
- name: Install dependencies
- run: npm ci
`
Check package requirements:
// package.json
{
"engines": {
"node": ">=18.0.0",
"npm": ">=9.0.0"
}
}CI Caching Configuration
GitHub Actions Cache
```yaml - name: Setup Node uses: actions/setup-node@v4 with: node-version: '18' cache: 'npm'
- name: Install dependencies
- run: npm ci
`
Custom Cache Strategy
```yaml - name: Cache node modules uses: actions/cache@v4 id: cache-npm with: path: | ~/.npm node_modules key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} restore-keys: | ${{ runner.os }}-node-
- name: Install dependencies
- if: steps.cache-npm.outputs.cache-hit != 'true'
- run: npm ci
`
GitLab CI Cache
```yaml cache: key: files: - package-lock.json paths: - node_modules/
install: script: - npm ci ```
CircleCI Cache
```yaml steps: - restore_cache: keys: - node-v1-{{ checksum "package-lock.json" }} - node-v1-
- run: npm ci
- save_cache:
- key: node-v1-{{ checksum "package-lock.json" }}
- paths:
- - node_modules
- - ~/.npm
`
Debugging Commands
```bash # Verbose output npm install --loglevel verbose
# Debug mode npm install --loglevel silly
# Dry run npm install --dry-run
# Check what would be installed npm ls --depth=0
# Check dependency tree npm ls package-name
# Check for duplicates npm dedupe
# Check outdated packages npm outdated
# Validate package.json npm pkg fix ```
Best Practices
Use npm ci in CI
```yaml # ALWAYS use npm ci in CI, not npm install - run: npm ci # Faster, uses lockfile, fails on mismatch
# NEVER use npm install in CI - run: npm install # Updates lockfile, slower, unpredictable ```
Lockfile Version
// package.json
{
"packageManager": "npm@9.0.0"
}Prevent Uncommitted Changes
- name: Check lockfile
run: |
npm install --package-lock-only
git diff --exit-code package-lock.jsonQuick Reference
| Error | Solution |
|---|---|
| E404 Not Found | Check registry and package name |
| ERESOLVE conflict | Use --legacy-peer-deps |
| EACCES permission | Fix npm permissions |
| ETIMEDOUT | Increase timeout, use mirror |
| ENOENT git | Setup git credentials |
| Lockfile mismatch | Use npm ci |
Prevention Tips
- 1.Always use
npm ciin CI, notnpm install - 2.Commit package-lock.json to repository
- 3.Use caching for faster installs
- 4.Pin Node version in CI
- 5.Configure proper registry authentication
- 6.Check for peer dependency conflicts early
Related Articles
- [GitHub Actions Workflow Failed](#)
- [Jenkins Build Failed](#)
- [Test Coverage Threshold Not Met](#)