无法找到模块“@angular-devkit/build-angular”。

17

我按照Angular CLI快速入门页面上的指南,使用npm完成了安装。

Angular CLI快速入门

在创建并进入我的新项目“frontend”后运行ng serve --open出现了以下错误:

Could not find module "@angular-devkit/build-angular" from "C:\\Users\\Brandon\\project-name\\frontend".
Error: Could not find module "@angular-devkit/build-angular" from "C:\\Users\\Brandon\\project-name\\frontend".
 at Object.resolve (C:\Users\Brandon\project-name\node_modules\@angular-devkit\core\node\resolve.js:141:11)
 at Observable.rxjs_1.Observable [as _subscribe] (C:\Users\Brandon\project-name\node_modules\@angular-devkit\architect\src\architect.js:132:40)

我已经尝试了类似于我的问题的其他问题中的建议,但是它没有起作用。答案是运行 npm install --save-dev @angular-devkit/build-angular

类似的问题

我还删除了模块、清除了缓存,然后进行了安装,但也没有起作用。

package.json:

{
 "name": "frontend",
 "version": "0.0.0",
 "scripts": {
   "ng": "ng",
   "start": "ng serve",
   "build": "ng build",
   "test": "ng test",
   "lint": "ng lint",
   "e2e": "ng e2e"
 },
"private": true,
"dependencies": {
  "@angular/animations": "^6.0.2",
  "@angular/common": "^6.0.2",
  "@angular/compiler": "^6.0.2",
  "@angular/core": "^6.0.2",
  "@angular/forms": "^6.0.2",
  "@angular/http": "^6.0.2",
  "@angular/platform-browser": "^6.0.2",
  "@angular/platform-browser-dynamic": "^6.0.2",
  "@angular/router": "^6.0.2",
  "core-js": "^2.5.4",
  "rxjs": "^6.0.0",
  "zone.js": "^0.8.26"
 },
"devDependencies": {
  "@angular/compiler-cli": "^6.0.2",
  "@angular-devkit/build-angular": "~0.6.3",
  "typescript": "~2.7.2",
  "@angular/cli": "^6.0.3",
  "@angular/language-service": "^6.0.2",
  "@types/jasmine": "~2.8.6",
  "@types/jasminewd2": "~2.0.3",
  "@types/node": "~8.9.4",
  "codelyzer": "~4.2.1",
  "jasmine-core": "~2.99.1",
  "jasmine-spec-reporter": "~4.2.1",
  "karma": "~1.7.1",
  "karma-chrome-launcher": "~2.2.0",
  "karma-coverage-istanbul-reporter": "~1.4.2",
  "karma-jasmine": "~1.1.1",
  "karma-jasmine-html-reporter": "^0.2.2",
  "protractor": "~5.3.0",
  "ts-node": "~5.0.1",
  "tslint": "~5.9.1"
 }
}

angular.json:

 {
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "frontend": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/frontend",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "frontend:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "frontend:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "frontend:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles.css"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "frontend-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "frontend:serve"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "frontend"
}

有些东西可以看一下,看看是否有任何帮助:https://github.com/angular/angular-cli/issues/10661 - R. Richards
在继续之前,我会删除锁定文件和 rm -r node_modules 并重新安装 npm i。我会关注这个问题,并稍后提供更多帮助。 - Ben Racicot
@Brandon Lumsden你修好了吗? - sinan
16个回答

20

创建并进入新项目 "frontend" 后运行 ng serve --open 给出了这个错误:

在创建项目后,您需要运行

npm install 

安装 package.json 中列出的所有依赖项


12

我查看了node_modules文件夹,发现只有依赖项被安装了,而没有开发环境需要的依赖项。在新版NPM中,我们需要明确地获取这些依赖项:

npm i --only=dev

如果您只想安装devkit:

npm install @angular-devkit/build-angular

与 ken107(github) 共同构思


5
如果您正在使用 Angular 8 版本,请运行以下命令以修复此问题。
ng update @angular/cli @angular/core

在提问时,请将所有代码放入代码块中:通常,缩进所有代码行4个空格,或用3个反引号(`` )括起代码块。对于与非代码相同行的内联代码,请在每侧使用单个反引号()。 - CertainPerformance

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

安装@angular-devkit/build-angular作为开发依赖项。这个包是在Angular 6.0中新引入的。

2
你应该添加这个命令的作用以及它与 OP 所做的不同之处(--save-dev,非全局)。 - m02ph3u5
2
虽然这段代码可能回答了问题,但提供有关它如何以及/或为什么解决问题的附加上下文将改善答案的长期价值。 - undetected Selenium
@Learner 请尝试使用以下命令:yarn add @angular-devkit/build-angular --dev - Syed Kashif
@kashif:老兄,我不知道Yarn是什么鬼。这个命令在cmd中无法运行。 - Jasmine

3

使用

npm update

或者运行

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


2

另一个问题可能与您的开发依赖项有关。请检查它们是否已正确安装(检查它们是否在node_modules文件夹中可用)。

如果没有,快速修复方法如下:

npm i --only=dev

或者检查您的npm设置是否针对生产环境:

npm config get production

如果它们被设置为true,请将它们更改为false:
npm config set -g production false

并设置一个新的Angular项目。

在这里找到提示: https://github.com/angular/angular-cli/issues/10661 (ken107和lichunbin814)

希望这有所帮助。


2
这对我很有帮助:在命令行中输入npm audit fix,然后我又能够使用ng serve --open了。"Original Answer"的翻译是"最初的回答"。

@Learner 只是分享在类似情况下对我有效的解决方法。其他答案当时对我无效,因此可能对某些人有所帮助。我绝不声称它总是解决此错误消息问题的方法。 - Rob Monhemius
这对我也起作用了。npm audit fix --force 作为最后的手段。 - Mandi

1
我刚遇到这个问题并已解决。我认为这个问题的根本原因是ng和当前版本的node.js(10.6.0)以及相应的npm不同步。我安装了LTS版本的node.js(8.11.3),问题消失了。

0

D:project/contactlist npm install 然后 D:project/contactlist ng new client

D:project/contactlist/client ng serve

对我来说这个方法有效,因为我不得不删除客户端文件夹并从contactlist文件夹开始npm安装。我尝试了所有的方法,甚至清除缓存,最终这个方法起作用了。


0

我刚刚也遇到了同样的问题。我的项目是使用 Angular-CLI 的 v1.6.0 版本生成的。

通过执行以下命令解决了问题:

npm update -g @angular/cli

编辑 package.json 文件,将以下行:

"@angular/cli": "1.6.0",

修改为:

"@angular/cli": "^1.6.0",

然后再次执行 npm update 命令即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接