You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular执行ng s时报错compiler_1.createR3ProviderExpression is not a function

Angular启动报错:TypeError: compiler_1.createR3ProviderExpression is not a function 解决方案

问题背景

执行npm i后运行ng s时触发如下错误:

Generating browser application bundles (phase: setup)...Compiling @angular/core : es2015 as esm2015
Compiling @angular/animations : es2015 as esm2015
Error: Error on worker #1: TypeError: compiler_1.createR3ProviderExpression is not a function

已尝试针对该错误的常规社区解决方案,但均未解决问题。

依赖版本问题分析

从提供的package.json依赖片段来看,核心问题是Angular构建工具与CLI/编译器版本不匹配:

  • @angular-devkit/build-angular版本为^12.0.2,但@angular/cli、@angular/compiler-cli等核心工具均固定为12.0.0
  • Angular生态的核心工具版本必须严格一致,版本差异会导致内部API不兼容,createR3ProviderExpression是Angular编译器内部API,版本不匹配会触发函数找不到的报错

解决步骤

  1. 统一核心工具版本
    修改package.json中@angular-devkit/build-angular的版本为12.0.0(与其他Angular核心工具版本完全一致),移除^符号锁定版本:

    "@angular-devkit/build-angular": "12.0.0",
    
  2. 彻底清理并重装依赖
    执行命令清除旧依赖及锁文件,重新安装:

    rm -rf node_modules package-lock.json
    npm install
    
  3. 清除Angular构建缓存
    清理CLI缓存避免旧构建文件干扰:

    ng cache clean
    
  4. 重启项目

    ng serve
    

额外排查(若上述步骤无效)

如果问题仍存在,可执行npm ls @angular/core排查是否有第三方依赖引入了不一致的Angular版本,若有可通过package.json的resolutions字段(npm/yarn)或pnpm.overrides(pnpm)强制锁定所有子依赖的Angular版本为12.0.0。

内容的提问来源于stack exchange,提问作者Tùng Lê Xuân

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 17:03:21