Module not found: Error: Can’t resolve ‘./$$_gendir/app/app.module.ngfactory’

昨日在ng4项目运行ng build --prod打包报错;
运行环境:

@angular/cli: 1.2.4
node: 8.2.1
os: win32 x64
@angular/animations: 4.3.1
@angular/common: 4.3.1
@angular/compiler: 4.3.1
@angular/core: 4.3.1
@angular/forms: 4.3.1
@angular/http: 4.3.1
@angular/platform-browser: 4.3.1
@angular/platform-browser-dynamic: 4.3.1
@angular/router: 4.3.1
@angular/cli: 1.2.4
@angular/compiler-cli: 4.3.1
@angular/language-service: 4.3.1

错误代码:

$ ng build --prod
 10% building modules 5/7 modules 2 active ...r\j1241.cn\resources\src\polyfills.tsThe final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string                                               Hash: 62995419c79332bcbbee                                                         Time: 8363ms
chunk    {0} polyfills.9e60c82de05f790de253.bundle.js (polyfills) 177 kB {4} [initial] [rendered]
chunk    {1} main.c22b61509cde9fcee2d3.bundle.js (main) 1.1 kB {3} [initial] [rendered]
chunk    {2} styles.d41d8cd98f00b204e980.bundle.css (styles) 69 bytes {4} [initial] [rendered]
chunk    {3} vendor.f7457d5ac1e9743fd76f.bundle.js (vendor) 849 kB [initial] [rendered]
chunk    {4} inline.484839083bfb4d7733fc.bundle.js (inline) 0 bytes [entry] [rendered]

ERROR in ./src/main.ts
Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in 'D:\other\j.cn\resources\src'
 @ ./src/main.ts 3:0-74
 @ multi ./src/main.ts

执行ng serve, ng build的时候均打包正常,真是让人百思不得其解。在github angular/cli上看到很多人提的isssues关于这个问题,确定这个错误是由于enhanced-resolve这个包升级后导致,于是跟着别人的答案尝试解决,但是均无效果,有的说ng-cli版本问题,有的说执行ng build --prod -aot=false,有的说安装enhanced-resolve 3.3.0这个版本等等,但是对我的项目并没有效果,很难知道到底是哪个包依赖了这个包,经过一系列的排查,摸索了一天,最后终于解决了这个问题。下面是具体的办法。

使用yarn解决包版本问题,yarn的相关文档请看这里:https://yarn.bootcss.com/docs/
yarn和npm的区别参考这里: https://zhuanlan.zhihu.com/p/23493436

第一步:安装yarn
第二步:删除node_module
第三步:运行yarn install安装依赖

这时候生成了一个yarn.lock文件,找到这个文件有关enhanced-resolve的地方,把不是3.3.0的地方改成3.3.0,对应的resolved地址改为3.3.0版本的地址:https://registry.yarnpkg.com/enhanced-resolve/-/enhanced-resolve-3.3.0.tgz#950964ecc7f0332a42321b673b38dc8ff15535b3 再次执行yarn i && ng build --prod,这下就行了,完美解决!

作者:大田角
链接:https://www.jianshu.com/p/2ccb909a1272
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。


如果给你带来帮助,欢迎微信或支付宝扫一扫,赞一下。
 

关于 “Module not found: Error: Can’t resolve ‘./$$_gendir/app/app.module.ngfactory’” 的 1 个意见

评论关闭。