【问题标题】:I get this error:Cannot find module '@angular-devkit/build-angular/package.json when I try run my app with ng serve from Linux我收到此错误:当我尝试从 Linux 使用 ng serve 运行我的应用程序时,找不到模块 '@angular-devkit/build-angular/package.json
【发布时间】:2020-06-09 18:46:08
【问题描述】:

大家好,当我尝试从终端(linux)运行我的应用程序时,当我运行此命令时,我遇到了下一个错误:

服务

我在终端中收到此错误:

AppBlog/blog# ng serve
An unhandled exception occurred: Cannot find module '@angular-devkit/build-angular/package.json'
Require stack:
- /usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/architect/node/node-modules-architect-host.js
- /usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/architect/node/index.js
- /usr/local/lib/node_modules/@angular/cli/models/architect-command.js
- /usr/local/lib/node_modules/@angular/cli/commands/serve-impl.js
- /usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/schematics/tools/export-ref.js
- /usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/schematics/tools/index.js
- /usr/local/lib/node_modules/@angular/cli/utilities/json-schema.js
- /usr/local/lib/node_modules/@angular/cli/models/command-runner.js
- /usr/local/lib/node_modules/@angular/cli/lib/cli/index.js
- /usr/local/lib/node_modules/@angular/cli/lib/init.js
- /usr/local/lib/node_modules/@angular/cli/bin/ng
See "/tmp/ng-ev5aDi/angular-errors.log" for further details.

然后我检查了这部分:

发生未处理的异常:找不到模块'@angular-devkit/build-angular/package.json'

这部分说找不到名为:“'@angular-devkit/build-angular/package.json'”的模块

然后我在我的项目中检查了这个文件,但我有这个,看:

  "devDependencies": {
    "@angular-devkit/build-angular": "~0.900.3",
    "@angular/cli": "~9.0.3",
    "@angular/compiler-cli": "~9.0.2",
    "@angular/language-service": "~9.0.2",
    "@types/node": "^12.11.1",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "^5.1.2",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.0",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.2",
    "protractor": "~5.4.3",
    "ts-node": "~8.3.0",
    "tslint": "~5.18.0",
    "typescript": "~3.7.5"
  }

然后我去谷歌搜索,我从stackoverflow 中找到了这个,我尝试了他们的解决方案,但它不起作用,我再次运行命令 ng serve。

我做错了什么?

在没有之前,在我添加 3 个组件之前:登录、注册和 home 在我的文件夹中:src/app 使用此命令:ng generate component login 然后我导入我的我的文件中的组件:app.component.ts 这样:

/* THIS IS JUST A EXAMPLE VERY SIMILAR TO MY CODE REAL,  I just import one component*/

import { Component } from '@angular/core';
import { loginComponent } from './login.component';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  declarations: [loginComponent], //import my login
})
export class AppComponent {
  title = 'blog';
}

也许是这部分,但我没有找到解决方案,任何角度专家都可以告诉我我做错了什么?

更新

我继续在谷歌搜索,我发现了这个: github/Angular 他们说我会再次安装 npm/angular-cli/node 并运行此命令来更新我的 angular cli:

卸载 ang-cli

npm 卸载 @angular/cli

安装了最新的

npm install -g @angular/cli@latest

但首先我用这个命令更新我的 npm:

安装了最新的 npm

npm install -g npm@latest

但还是不行。

【问题讨论】:

  • 您是否尝试重新安装模块?也降级到以下版本并检查
  • @prosper1 当我尝试使用以下命令更新或重新安装模块时:npm install --save-dev @angular-devkit/build-angular (stackoverflow.com/questions/59136899/…),我收到此错误或消息我的意思是: npm install --save-dev @angular-devkit/build-angular
  • 您是否尝试删除文件夹“node_modules”并在其他时间使用 npm install --save 安装所有依赖项?
  • 您在 UPDATE 中描述的解决方案对我有用。

标签: angular


【解决方案1】:

在我的例子中,这两个命令有帮助:

npm install -g npm@latest
npm install -g @angular/cli

【讨论】:

    【解决方案2】:

    请尝试在项目文件夹中安装 npm。我已经在全球范围内安装了 npm,但在解决方案文件夹上再次安装 npm 解决了我的问题。

    npm install 在项目文件夹中可以解决问题。然后做ng serve -o

    【讨论】:

    • npm install 在解决方案文件夹中可以解决问题。然后做ng serve -o
    • “ng serve -o”中的“-o”有什么作用?
    【解决方案3】:

    我最近将 Angular CLI 升级到 Angular 9 时遇到了这个问题。 以下命令解决了这个问题。

    npm install --save-dev @angular-devkit/build-angular
    npm install
    ng serve -o
    

    【讨论】:

    • 当我这样做时,我得到以下错误npm ERR! Unexpected end of JSON input while parsing near '...@types/node/-/node-6.' npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\rmahapatra\AppData\Roaming\npm-cache\_logs\2020-08-11T11_04_56_358Z-debug.log
    【解决方案4】:

    npm install 在您的终端中运行它并完成。

    【讨论】:

      【解决方案5】:

      在我的例子中,有 2 个命令有帮助:

      npm install --save-dev @angular-devkit/build-angular

      npm install --save-dev @angular-devkit/core

      【讨论】:

        【解决方案6】:

        在项目目录下运行以下命令

        npm install --save-dev @angular-devkit/build-angular
        

        【讨论】:

          【解决方案7】:

          错误指出“找不到模块'@angular-devkit/build-angular/package.json'”,这意味着您必须添加该包。

          1. 安装最新的 npm

            npm install -g npm@latest

          2. 添加缺失的

            npm install --save-dev @angular-devkit/build-angular

          【讨论】:

            【解决方案8】:

            在现有的 Angular 10 项目中遇到了这个确切的问题。以下步骤为我解决了这个问题:

            1. 删除package-lock.json文件。
            2. 删除node_modules 文件夹及其所有内容。
            3. 运行npm install。请注意,在我的情况下,我必须添加 --force 标志。 YMMV

            此时,ng build --prod 为我成功完成。

            【讨论】:

              【解决方案9】:

              在我的情况下,在项目文件夹中运行 npm install 解决了这个问题。

              【讨论】:

                【解决方案10】:

                当我更新 Angular Cli 时,我正在工作 Angular 项目,然后出现此错误 发生未处理的异常:找不到模块'@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/utils' 需要堆栈:

                • C:\codeFlies Development\how_marketsapp\node_modules@ionic\angular-toolkit\builders\utils\index.js
                • C:\codeFlies Development\how_marketsapp\node_modules@ionic\angular-toolkit\builders\cordova-build\index.js
                • C:\codeFlies Development\how_marketsapp\node_modules@angular-devkit\architect\node\node-modules-architect-host.js
                • C:\codeFlies Development\how_marketsapp\node_modules@angular-devkit\architect\node\index.js
                • C:\codeFlies Development\how_marketsapp\node_modules@angular\cli\models\architect-command.js
                • C:\codeFlies Development\how_marketsapp\node_modules@angular\cli\commands\run-impl.js
                • C:\codeFlies Development\how_marketsapp\node_modules@angular\cli\node_modules@angular-devkit\schematics\tools\export-ref.js
                • C:\codeFlies Development\how_marketsapp\node_modules@angular\cli\node_modules@angular-devkit\schematics\tools\index.js
                • C:\codeFlies Development\how_marketsapp\node_modules@angular\cli\utilities\json-schema.js
                • C:\codeFlies Development\how_marketsapp\node_modules@angular\cli\lib\cli\index.js
                • C:\codeFlies Development\how_marketsapp\node_modules@angular\cli\lib\init.js
                • C:\codeFlies Development\how_marketsapp\node_modules@angular\cli\bin\ng 有关详细信息,请参阅“C:\Users\hp\AppData\Local\Temp\ng-UQJuET\angular-errors.log”。

                我已经使用这个命令解决了这个错误

                npm i @ionic/angular-toolkit@latest

                【讨论】:

                  【解决方案11】:

                  我终于找到了适合我的东西。

                  在你的 npm 命令末尾添加--force

                  点赞npm install --save-dev @angular-devkit/build-angular --force

                  然后ng serve

                  【讨论】:

                    【解决方案12】:

                    如果您使用 Angular 11 并遇到此问题,请执行以下操作:

                    1. devDependencies

                      中移除 "@angular-devkit/build-ng-packagr:build" 依赖
                    2. 更新 angular.json->projects->your-library-name -> 架构师 -> 构建 ->

                      旧:"builder": "@angular-devkit/build-ng-packagr:build",

                      新功能:"builder": "@angular-devkit/build-angular:ng-packagr",

                    【讨论】:

                    • 我可能使用的是旧版本 9。但我在这里使用了“@angular-devkit/build-ng-packagr:build”并且它工作正常。感谢 angular.json 中有关构建器的提示
                    【解决方案13】:

                    就我而言,我遇到了类似但不同的错误。

                    Cannot find module '@nguniversal/builders/package.json'
                    

                    要修复它,我必须运行

                    npm install --save-dev @nguniversal/builders
                    

                    【讨论】:

                      【解决方案14】:

                      我修复了它:

                      npm i --save-dev @angular-devkit/build-angular --force
                      npm i @angular/compiler-cli --save-dev
                      

                      【讨论】:

                        【解决方案15】:

                        前面有错误:-

                        ng serve

                        以下对我有用:-

                        npm install npm@latest 不用-g重新安装

                        Packages installed successfully

                        【讨论】:

                        • 请尽量避免使用带有代码和错误的图像,而是将其作为代码放在您的答案中
                        【解决方案16】:

                        这也发生在我身上,当时我ng serve在公司的克隆代码上。发生这种情况是因为节点模块安装出错,所以我们需要重新安装、构建并提供服务。

                        1. npm install --save-dev @angular-devkit/build-angular
                        2. ng 构建
                        3. ng 服务

                        如果上面的第一步不起作用,因为它对我不起作用并收到另一个错误:“npm ERR!cb() never call!” 我使用这个命令解决了这个问题:npm cache clean --force 一旦成功,请继续执行步骤 2-3。它奏效了。

                        P.S 如果可行,请给我一个支持,我的帐户是 stackoverlfow hehez 中的新帐户。

                        【讨论】:

                          猜你喜欢
                          • 1970-01-01
                          • 2020-01-14
                          • 2021-05-24
                          • 2020-12-28
                          • 2020-09-11
                          • 2019-12-08
                          • 2021-03-26
                          • 2018-10-28
                          相关资源
                          最近更新 更多