2017-10-18 79 views
5

运行ng build我的角4项目给这个错误:角可供依赖型无模块厂:ContextElementDependency

 14% building modules 40/46 modules 6 active ...es\@angular\http\@angular\http.es5.js 
An error occured during the build: 
Error: No module factory available for dependency type: ContextElementDependency 
    at Compilation.addModuleDependencies (D:\dev\workspace\rep\node_modules\@angular\cli\node_modules\webpack\lib\Compilation.js:213:21) 
    at Compilation.processModuleDependencies (D:\dev\workspace\rep\node_modules\@angular\cli\node_modules\webpack\lib\Compilation.js:202:8) 
    at _this.buildModule.err (D:\dev\workspace\rep\node_modules\@angular\cli\node_modules\webpack\lib\Compilation.js:350:14) 
    at building.forEach.cb (D:\dev\workspace\rep\node_modules\@angular\cli\node_modules\webpack\lib\Compilation.js:147:27) 
    at Array.forEach (native) 
    at callback 

我已经在GitHub上&计算器在这个问题上读了许多Q/A,但非他们帮助了我。

由于答案建议我已经删除了webpack,但这并没有帮助。删除了node_modules,从package.json中删除了webpack,运行npm install,仍然没有帮助。清理npm缓存,从package.json中移除webpack,运行npm install,仍然没有结果。许多其他类似的建议也没有帮助。

当我的package.json删除的WebPack和运行npm install我得到以下几点:

Cannot find module 'webpack/lib/node/NodeTemplatePlugin' Error: Cannot 
find module 'webpack/lib/node/NodeTemplatePlugin' 
    at Function.Module._resolveFilename (module.js:469:15) 
    at Function.Module._load (module.js:417:25) 
    at Module.require (module.js:497:17) 
    at require (internal/module.js:20:19) 
    at Object.<anonymous> (D:\dev\workspace\rep\node_modules\html-webpack-plugin\lib\compiler.js:11:26) 
    at Module._compile (module.js:570:32) 
    at Object.Module._extensions..js (module.js:579:10) 
    at Module.load (module.js:487:32) 
    at tryModuleLoad (module.js:446:12) 
    at Function.Module._load (module.js:438:3) 
    at Module.require (module.js:497:17) 
    at require (internal/module.js:20:19) 
    at Object.<anonymous> (D:\dev\workspace\rep\node_modules\html-webpack-plugin\index.js:7:21) 
    at Module._compile (module.js:570:32) 
    at Object.Module._extensions..js (module.js:579:10) 
    at Module.load (module.js:487:32) 

当返回的WebPack到的package.json,运行npm install然后运行npm list webpack我得到以下结果:

+-- @angular/[email protected] 
| `-- [email protected] 
`-- [email protected] 

下面是该项目的package.json:

{ 
    "name": "somename", 
    "version": "1.0.0", 
    "description": "", 
    "author": "", 
    "url": "", 
    "copyright": "somec", 
    "license": "MIT", 
    "scripts": { 
    "ng": "ng", 
    "start": "ng serve", 
    "build": "ng build", 
    "test": "ng test", 
    "lint": "ng lint", 
    "e2e": "ng e2e" 
    }, 
    "private": true, 
    "dependencies": { 
    "@angular/common": "^4.4.5", 
    "@angular/compiler": "^4.4.5", 
    "@angular/core": "^4.4.5", 
    "@angular/forms": "^4.4.5", 
    "@angular/http": "^4.4.5", 
    "@angular/platform-browser": "^4.4.5", 
    "@angular/platform-browser-dynamic": "^4.4.5", 
    "@angular/router": "^4.4.5", 
    "@angular/upgrade": "^4.4.5", 
    "amazon-cognito-identity-js": "^1.21.0", 
    "chart.js": "2.7.0", 
    "core-js": "2.5.1", 
    "font-awesome": "^4.7.0", 
    "jquery": "^3.2.1", 
    "moment": "2.18.1", 
    "ng2-charts": "1.6.0", 
    "ngx-bootstrap": "1.9.3", 
    "raw-loader": "^0.5.1", 
    "rxjs": "5.4.3", 
    "simple-line-icons": "^2.4.1", 
    "ts-helpers": "1.1.2", 
    "zone.js": "0.8.17" 
    }, 
    "devDependencies": { 
    "@angular/cli": "^1.4.7", 
    "@angular/compiler-cli": "^4.4.5", 
    "@types/jasmine": "2.6.0", 
    "@types/jquery": "^3.2.13", 
    "@types/node": "8.0.28", 
    "codelyzer": "3.2.0", 
    "jasmine-core": "2.8.0", 
    "jasmine-spec-reporter": "4.2.1", 
    "karma": "1.7.1", 
    "karma-chrome-launcher": "2.2.0", 
    "karma-cli": "1.0.1", 
    "karma-coverage-istanbul-reporter": "1.3.0", 
    "karma-jasmine": "1.1.0", 
    "karma-jasmine-html-reporter": "0.2.2", 
    "node-sass": "^4.5.3", 
    "postcss-loader": "^2.0.6", 
    "protractor": "5.1.2", 
    "sass-loader": "^6.0.6", 
    "ts-node": "3.3.0", 
    "tslint": "5.7.0", 
    "typescript": "2.5.2", 
    "webpack": "^3.6.0" 
    }, 
    "engines": { 
    "node": ">= 6.9.0", 
    "npm": ">= 3.0.0" 
    } 
} 

当我在其他机器上克隆这个回购时,运行npm install,然后ng build没问题,工作正常,但是在我的系统中出现这个错误。我试过从我的机器上彻底删除回购,从头开始克隆并运行npm installng build,仍然是同样的错误。

有人可以告诉什么可以是这种行为的原因,以及如何解决它?

回答

15

步骤我参加了,如果有人遇到它的情况下,来解决这个问题:

  1. 的package.json:从DevDependencies
  2. 删除的WebPack RM -R node_modules(删除node_modules文件夹)
  3. NPM我 - 克的WebPack
  4. NPM我-g的WebPack-DEV-服务器
  5. 删除包-lock.json(如果它的存在)
  6. NPM我
  7. NPM启动

可是我还是不完全理解的原因,发生这种情况

+0

谢谢!大多数其他修补程序只需删除node_modules,然后清除缓存并重新安装。看起来像webpack的全球安装为我做了。 – Nayfin

+0

@Nayfin欢迎您! – komron

1

npm ls webpack

如果你看到的webpack两个版本(下@角/ CLI和根node_modules)那么这就是问题所在。删除/重命名@angular/cli.bin文件夹下的webpack @angular/cli

问题解决了,我

+0

但如果你更新/重新安装@ angular/cli,你会再次遇到这个问题,因为它会再次安装webpack – komron

+0

这就是为什么你应该从开发依赖关系中删除。这是你不要删除包,只是为了重新安装它们 – phacic

+0

这是我的问题。从devDependencies中删除并使用我的全球版本的webpack解决了这个问题。 – ssmith