Resolving Error: Cannot find module '@nx/nx-linux-x64-gnu'
Problem Statement
When building Angular projects using Nx in CI environments like GitHub Actions, you may encounter installation errors blocking your pipeline. The core error typically appears as:
Error: Cannot find module '@nx/nx-linux-x64-gnu'
NX Missing Platform Dependency
The Nx CLI could not find or load the native binary for your supported platform (linux-x64).This happens because:
- Nx downloads platform-specific binaries (
@nx/nx-linux-x64-gnu) as optional dependencies - In CI environments, npm automatically skips optional dependencies (
--omit=optional) - The missing binary prevents Nx from executing in your pipeline
Recommended Solutions
Solution 1: Install Optional Dependencies Explicitly (Preferred)
- Add platform-specific dependencies (
npm i -D) to yourpackage.json:
{
// ... existing dependencies ...
"devDependencies": {
"@nx/nx-linux-x64-gnu": "^18.0.4",
// Include additional platforms as needed:
"@nx/nx-darwin-arm64": "^18.0.4",
"@nx/nx-darwin-x64": "^18.0.4",
"@nx/nx-win32-x64-msvc": "^18.0.4"
}
}TIP
Use the same version as your main Nx package. Replace 18.0.4 with your actual Nx CLI version.
- Update CI scripts to install optional dependencies:
- name: Install dependencies
run: npm ci --include=optional # For 'npm ci'
# or: npm install --include=optional # For 'npm install'Solution 2: Clean Install & Rebuild Dependencies
If solution 1 doesn't resolve the issue:
# Clean existing artifacts
npm cache clean --force
rm -rf node_modules package-lock.json
# Full reinstall
npm install --include=optionalWARNING
This approach regenerates package-lock.json. Review lockfile changes in version control afterward.
CI Configuration Examples
GitHub Actions Workflow
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Install dependencies
run: |
npm ci --include=optionalDockerfile Implementation
FROM node:20-alpine
WORKDIR /app
COPY package*.json .
RUN npm ci --include=optional # Ensure optional deps are installed
COPY . .
RUN npm run buildWhy These Solutions Work
Explicit Dependency Declaration
Adding@nx/nx-linux-x64-gnuas a dev dependency guarantees npm always downloads the platform-specific binary.Including Optional Dependencies
The--include=optionalflag overrides npm's default CI behavior that skips these crucial packages.Cache Cleaning
Forces npm to refetch package metadata, resolving cases where outdated cache references incorrect package versions.
Additional Considerations
- npm Version Compatibility:
--include=optionalrequires npm ≥9 - Platform Binaries: Ensure declared platforms match your CI environment
- NX Troubleshooting Guide: For advanced scenarios, consult the official Nx troubleshooting documentation
Common Pitfall
Avoid downgrading npm (a common outdated suggestion). Nx v18+ requires npm ≥9. Explicitly including optional dependencies has been the recommended solution since 2024.
These solutions resolve platform-specific binary installation issues while maintaining deterministic builds in CI environments.