Angular 6 Docker构建报错:TypeScript版本不符合编译器要求
Looks like you've hit a classic dependency version mismatch between Angular and TypeScript. Let's break down what's happening and fix it step by step.
What's Causing the Error?
The error message spells it out clearly:
ERROR in The Angular Compiler requires TypeScript >=2.7.2 and <2.8.0 but 2.8.3 was found instead.
In your package.json, you specified "typescript": "^2.7.2". The ^ symbol tells npm to install the latest version compatible with the major/minor release (so any version >=2.7.2 and <3.0.0). Since TypeScript 2.8.3 falls into that range, npm pulled it in—but Angular 6's compiler doesn't support TypeScript versions beyond 2.7.x.
Solutions
1. Lock TypeScript to an Exact Version
Modify the typescript entry in your package.json's devDependencies to remove the ^ symbol. This forces npm to install exactly version 2.7.2, no more no less:
"typescript": "2.7.2"
2. Restrict to 2.7.x Minor Versions
If you want to allow bugfix updates within the 2.7.x branch (like 2.7.3), use the ~ symbol instead of ^. This limits installs to versions >=2.7.2 and <2.8.0, which perfectly matches Angular's requirement:
"typescript": "~2.7.2"
3. Use package-lock.json to Pin All Dependencies
For more robust dependency locking (ensuring all dependencies stay consistent across every build), generate a package-lock.json file locally first:
npm install
This file records the exact version of every dependency you're using. Then update your Dockerfile to copy this file alongside package.json before running npm install—this also leverages Docker's cache to speed up future builds:
# Stage 0 - Pre-requisite: Build and compile the Angular app FROM node:8.11.2 as node WORKDIR /app # Copy lock file first to cache dependencies COPY package.json package-lock.json /app/ RUN npm install COPY ./ /app/ ARG env=production RUN npm run build -- --configuration $env # Stage 1 - Production-ready build with Nginx FROM nginx:1.14 COPY --from=node /app/dist/ /usr/share/nginx/html COPY ./nginx-custom.conf /etc/nginx/conf.d/default.conf
After applying any of these fixes, re-run your Docker build command:
docker build -t demo:production .
内容的提问来源于stack exchange,提问作者Kay

