加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

ERROR in AppModule is not an NgModule

发布时间:2020-12-17 09:31:26 所属栏目:安全 来源:网络整理
导读:angularjs2的环境问题解决了好久。 百度到的答案也是各种各样还解决不了我的问题。 好在这几天经过不断的测试终于给解决了。 ERROR in AppModule is not an NgModuleERROR in ./src/main.tsModule build failed: TypeError: Cannot read property 'newLine'

angularjs2的环境问题解决了好久。

百度到的答案也是各种各样还解决不了我的问题。

好在这几天经过不断的测试终于给解决了。

ERROR in AppModule is not an NgModule


ERROR in ./src/main.ts
Module build failed: TypeError: Cannot read property 'newLine' of undefined
    at Object.getNewLineCharacter (d:webmyNgng2node_modulestypescriptlibtypescript.js:8062:20)
    at Object.createCompilerHost (d:webmyNgng2node_modulestypescriptlibtypescript.js:44978:26)
    at Object.ngcLoader (d:webmyNgng2node_modules@ngtoolswebpacksrcloader.js:338:33)
 @ multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts


ERROR in ./src/polyfills.ts
Module build failed: TypeError: Cannot read property 'newLine' of undefined
    at Object.getNewLineCharacter (d:webmyNgng2node_modulestypescriptlibtypescript.js:8062:20)
    at Object.createCompilerHost (d:webmyNgng2node_modulestypescriptlibtypescript.js:44978:26)
    at Object.ngcLoader (d:webmyNgng2node_modules@ngtoolswebpacksrcloader.js:338:33)
 @ multi ./src/polyfills.ts
webpack: Failed to compile.

出现这个错误的原因是本地typescript版本冲突了。但是我不知道npm为什么会这样。网上说它的机制按理说不是会导致版本冲突的问题。没有研究过。

首先:

$ npm ls typescript

输入这个命令查看项目的typescript版本和依赖。

发现出现2个不同的版本在各个地方了。

由于angularcli依赖的是2.1.6,这个是自动生成的,总不能去node_module里面去找package.json改吧。没法改。放弃。
ng2@0.0.0 d:webmyNgng2
+-- @angular/cli@1.0.0-rc.1 -> d:webmyNgng2node_modules.1.0.0-rc.1@@angularcli
| +-- @angular/compiler-cli@2.4.9
| | +-- @angular/tsc-wrapped@0.5.2
| | | `-- typescript@2.1.6 deduped extraneous
| | `-- typescript@2.1.6 extraneous
| +-- @angular/tsc-wrapped@0.5.2
| | +-- tsickle@0.2.6
| | | `-- typescript@2.1.6 extraneous
| | `-- typescript@2.1.6 extraneous
| +-- @ngtools/webpack@1.2.12
| | `-- typescript@2.1.6 extraneous
| `-- typescript@2.1.6
`-- typescript@2.0.10


那么另一个2.0.10是在项目的package.json中配置的。这个虽然是angular/cli生成的,但是是在我们自己的项目里面,所以很好改,我们把这里的typescript改成angular/cli自己依赖的那个版本。

"typescript": "~2.0.0"
把这个改成
"~2.1.0"
~表示npm会自动下载离他最近的一个小版本。比如我的电脑上面就是2.1.6。这样的话,就是angular/cli自己依赖的完全一致了。

再次

$ npm ls typescript

发现,版本一致了。
ng2@0.0.0 d:webmyNgng2
+-- @angular/cli@1.0.0-rc.1 -> d:webmyNgng2node_modules.1.0.0-rc.1@@angularcli
| `-- typescript@2.1.6
`-- typescript@2.1.6 -> d:webmyNgng2node_modules.2.1.6@typescript


$ ng serve
** NG Live Development Server is running on http://localhost:4200 **
Hash: b21dc5aadbb6f2b69213
Time: 10251ms
chunk {0} polyfills.bundle.js,polyfills.bundle.js.map (polyfills) 158 kB {4} [initial] [rendered]
chunk {1} main.bundle.js,main.bundle.js.map (main) 3.14 kB {3} [initial] [rendered]
chunk {2} styles.bundle.js,styles.bundle.js.map (styles) 9.84 kB {4} [initial] [rendered]
chunk {3} vendor.bundle.js,vendor.bundle.js.map (vendor) 2.6 MB [initial] [rendered]
chunk {4} inline.bundle.js,inline.bundle.js.map (inline) 0 bytes [entry] [rendered]
webpack: Compiled successfully.



搞定。在这里做个笔记,希望能帮到之后遇到的人。毕竟这玩意刚接触太浪费时间了。

对了,安装环境的时候记得用cnpm install。原生的npm install安装会报错,原因是node-sass被墙了。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读