首页 > 解决方案 > 使用“ng generate”或“ng add”时出错

问题描述

所以我在 Termux Android ARM 中使用 Angular 版本 8。这是来自 Angular CLI 的 Angular 版本信息:

$ ng version
    _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 8.0.2
Node: 12.4.0
OS: android arm
Angular: <error>
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

但是当我使用ng add命令为我的网站项目添加 PWA 和 service worker 时,它返回错误:

$ ng add @angular/pwa --project betaleapofazzamgithubio
Workspace needs to be loaded before it is used.
Error: Workspace needs to be loaded before it is used.
    at Workspace._assertLoaded (/data/data/com.termux/files/home/node_modules/@angular-devkit/core/src/experimental/workspace/workspace.js:108:19)
    at Workspace.getProjectByPath (/data/data/com.termux/files/home/node_modules/@angular-devkit/core/src/experimental/workspace/workspace.js:156:14)
    at getProjectByCwd (/data/data/com.termux/files/home/node_modules/@angular/cli/utilities/config.js:103:26)
    at Object.getConfiguredPackageManager (/data/data/com.termux/files/home/node_modules/@angular/cli/utilities/config.js:116:25)
    at Object.getPackageManager (/data/data/com.termux/files/home/node_modules/@angular/cli/utilities/package-manager.js:32:35)
    at new AddCommand (/data/data/com.termux/files/home/node_modules/@angular/cli/commands/add-impl.js:25:49)
    at Object.runCommand (/data/data/com.termux/files/home/node_modules/@angular/cli/models/command-runner.js:172:25)
    at async default_1 (/data/data/com.termux/files/home/node_modules/@angular/cli/lib/cli/index.js:32:31)

因为错误,我使用ng generate命令而不是ng add命令。但它也返回错误。这是错误:

$ ng generate service-worker
Workspace needs to be loaded before it is used.
Error: Workspace needs to be loaded before it is used.
    at Workspace._assertLoaded (/data/data/com.termux/files/home/node_modules/@angular-devkit/core/src/experimental/workspace/workspace.js:108:19)
    at Workspace.getProjectByPath (/data/data/com.termux/files/home/node_modules/@angular-devkit/core/src/experimental/workspace/workspace.js:156:14)
    at Object.getProjectByCwd (/data/data/com.termux/files/home/node_modules/@angular/cli/utilities/config.js:103:26)
    at GenerateCommand.getDefaultSchematicCollection (/data/data/com.termux/files/home/node_modules/@angular/cli/models/schematic-command.js:251:38)
    at GenerateCommand.parseSchematicInfo (/data/data/com.termux/files/home/node_modules/@angular/cli/commands/generate-impl.js:70:35)
    at GenerateCommand.initialize (/data/data/com.termux/files/home/node_modules/@angular/cli/commands/generate-impl.js:16:54)
    at GenerateCommand.validateAndRun (/data/data/com.termux/files/home/node_modules/@angular/cli/models/command.js:127:20)
    at async Object.runCommand (/data/data/com.termux/files/home/node_modules/@angular/cli/models/command-runner.js:178:24)
    at async default_1 (/data/data/com.termux/files/home/node_modules/@angular/cli/lib/cli/index.js:32:31)

请帮助解决此错误并修复此错误解决方案。

标签: angularangular-cli

解决方案


  1. 创建项目后切换到您的项目文件夹或工作区。
  2. npm install
  3. 运行以下命令ng generate component <componentName> //*for adding new component*ng generate service <serviceName> //*for adding new service*

推荐阅读