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

升级至Angular 6后编译报错:无法解析'http'模块求助

解决Angular 6升级后编译报错:Can't resolve 'http'

我之前帮朋友排查过一模一样的问题,大概率是依赖缓存或者版本不匹配导致的,试试下面这几个步骤,按顺序来:

步骤1:清理依赖缓存并重新安装

首先彻底删除本地依赖文件夹和锁文件,再重新安装依赖,避免旧缓存干扰:

# Mac/Linux
rm -rf node_modules package-lock.json yarn.lock
npm install

# Windows
rmdir /s /q node_modules
del package-lock.json yarn.lock
npm install

步骤2:检查并补全Webpack polyfill(若有自定义配置)

Webpack 4+不再默认包含Node.js核心模块的polyfill,而@angular-devkit/core里的某些代码依赖了http模块。如果你的项目用了自定义Webpack配置,需要手动添加polyfill配置:

// webpack.config.js
module.exports = {
  resolve: {
    fallback: {
      http: require.resolve('stream-http'),
      https: require.resolve('https-browserify'),
      stream: require.resolve('stream-browserify')
    }
  }
};

配置完后记得安装对应的依赖包:

npm install stream-http https-browserify stream-browserify --save-dev

步骤3:对齐@angular-devkit/core与CLI版本

有时候是工具链版本不兼容导致的,把@angular-devkit/core更新到和Angular CLI一致的版本:

npm install @angular-devkit/core@6.0.3 --save-dev

步骤4:清理Angular CLI缓存

最后清理Angular CLI的本地缓存,确保编译时加载最新的依赖:

ng cache clean

如果以上步骤都试过还是不行,可以试试重启IDE和终端——有时候缓存会残存在进程里,重启后就能解决。


内容的提问来源于stack exchange,提问作者Damian Roszyk

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:14:38