关于项目从angular4更新到5及angular/cli问题的说明
原项目使用的是angular4.2.4,angular/cli版本为1.4.9,在之前的使用中并没有问题。然而由于angular/cli的大版本更新(6.0),导致现在在项目中使用ng指令,如ng g c来创建新组件时,会报错如下:
Cannot read property 'NullLogger' of undefinedTypeError: Cannot read property 'NullLogger' of undefined at SchematicEngine.createContext (D:\Projects\demo\node_modules\@angular-devkit\schematics\src\engine\engine.js:64:38) at SchematicImpl.call (D:\Projects\demo\node_modules\@angular-devkit\schematics\src\engine\schematic.js:35:38) at Promise (D:\Projects\demo\node_modules\@angular\cli\tasks\schematic-run.js:72:23) at Class.run (D:\Projects\demo\node_modules\@angular\cli\tasks\schematic-run.js:71:16) at Class.run (D:\Projects\demo\node_modules\@angular\cli\commands\generate.js:149:33) at resolve (D:\Projects\demo\node_modules\@angular\cli\ember-cli\lib\models\command.js:261:20) at Class.validateAndRun (D:\Projects\demo\node_modules\@angular\cli\ember-cli\lib\models\command.js:240:12) at Promise.resolve.then.then (D:\Projects\demo\node_modules\@angular\cli\ember-cli\lib\cli\cli.js:140:24)
经查阅资料,原因是本地项目使用的angular/cli版本太低,于是开始升级angualr/cli的版本:
执行:npm install -g @angular/cli@latest
然后再运行,会报错:
Local workspace file ('angular.json') could not be found.Error: Local workspace file ('angular.json') could not be found. at WorkspaceLoader._getProjectWorkspaceFilePath (/home/travis/build/pradeep0601/Angular5-Router-App/node_modules/@angular/cli/models/workspace-loader.js:37:19) at WorkspaceLoader.loadWorkspace (/home/travis/build/pradeep0601/Angular5-Router-App/node_modules/@angular/cli/models/workspace-loader.js:24:21) at TestCommand._loadWorkspaceAndArchitect (/home/travis/build/pradeep0601/Angular5-Router-App/node_modules/@angular/cli/models/architect-command.js:177:32) at TestCommand. (/home/travis/build/pradeep0601/Angular5-Router-App/node_modules/@angular/cli/models/architect-command.js:45:25) at Generator.next () at /home/travis/build/pradeep0601/Angular5-Router-App/node_modules/@angular/cli/models/architect-command.js:7:71 at new Promise () at __awaiter (/home/travis/build/pradeep0601/Angular5-Router-App/node_modules/@angular/cli/models/architect-command.js:3:12) at TestCommand.initialize (/home/travis/build/pradeep0601/Angular5-Router-App/node_modules/@angular/cli/models/architect-command.js:44:16) at /home/travis/build/pradeep0601/Angular5-Router-App/node_modules/@angular/cli/models/command-runner.js:100:23
可能是由于angular/cli版本与项目不匹配导致。
于是,总结升级angular/cli步骤如下:
1.全局卸载angular/cli:
npm uninstall -g @angular/cli
2.清理:
npm cache verify
3.全局安装最新angular/cli:
(注:angular5项目最好安装angular/cli 1.7.4版本;npm install -g @angular/cli@1.7.4)
npm install -g @angular/cli@latest (这样安装的是angular/cli 6)
4.在项目路径下卸载angular/cli:
npm uninstall --save-dev @angular/cli
5.在项目路径下安装:
npm install --save-dev @angular/cli@1.7.4
6.项目路径下npm install或者yarn install
7.在npm安装后修复问题:
npm audit fix
8.解决与“angular.json”相关的问题:
ng update @angular/cli --migrate-only --from=1.4.9
在完成这些步骤之后,运行项目,会提示该angular/cli版本仅支持angular5.0及以上版本,于是还需要升级angular版本,此时应当看angular官方文档,会有升级说明,以及一个升级指导,链接如下:
由于此项目是从angular4.2.4升级到angular5.0,项目容量中等,且用yarn管理包,选择如下:
然后点击show me how to update,下面会列出更新要点;
这里会执行:
yarn add @angular/animations@^5.0.0 @angular/common@^5.0.0 @angular/compiler@^5.0.0 @angular/compiler-cli@^5.0.0 @angular/core@^5.0.0 @angular/forms@^5.0.0 @angular/http@^5.0.0 @angular/platform-browser@^5.0.0 @angular/platform-browser-dynamic@^5.0.0 @angular/platform-server@^5.0.0 @angular/router@^5.0.0 typescript@2.4.2 rxjs@^5.5.2
完成!现在可以运行项目了,而且可以正常创建新组件了。
参考:
travis ci - Error: Local workspace file ('angular.json') could not be found - Stack Overflow