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

Angular 16升级至18后运行时报错:webpackContext不是函数

Angular 16 升级至 18 后 import.meta.webpackContext 运行时错误解决方案

问题背景

Web应用Angular版本从16升级到18后,项目构建成功,但运行时抛出类型错误,import.meta对象中不存在webpackContext方法。

环境依赖

package.json中相关webpack依赖配置:

{
  "@ngtools/webpack": "^18.2.3",
  "@types/webpack": "^5.28.5",
  "webpack": "^5.94.0"
}

报错详情

运行时错误提示:

Uncaught TypeError: (intermediate value).webpackContext is not a function

触发错误的代码片段:

const context = (import.meta as any).webpackContext(
    '../../plugin/dist',
    { recursive: true, regExp: /\.(?:ogg|mp3|swf)$/, mode: 'lazy' }
);

解决方案

修改angular.json中的项目构建配置:

  • 将构建器字段从 "@angular-devkit/build-angular:application" 替换为 "@angular-devkit/build-angular:browser"
  • 将配置中的 "browser" 属性更改为 "main"

内容的提问来源于stack exchange,提问作者Adventitious Angles Qs Poster

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 16:57:15