angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Lazy loading module broken with " No module factory available for dependency type: ContextElementDependency"

Open dherges opened this issue 9 years ago • 58 comments

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request

Versions.

$ ng --version
    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/
@angular/cli: 1.1.0-rc.0
node: 6.10.0
os: win32 x64
@angular/common: 4.1.3
@angular/compiler: 4.1.3
@angular/core: 4.1.3
@angular/forms: 4.1.3
@angular/http: 4.1.3
@angular/platform-browser: 4.1.3
@angular/platform-browser-dynamic: 4.1.3
@angular/router: 4.1.3
@angular/cli: 1.1.0-rc.0
@angular/compiler-cli: 4.1.3
@angular/tsc-wrapped: 4.1.3

Repro steps.

Include a lazy loaded module in AppRoutingModule.

This doesn't happen with a fresh ng new <project> set-up. When adding "webpack": "^2.5.0" dependency, this breaks the build.

    "typescript": "~2.3.1",
    "webpack": "^2.5.0",
    "webpack-dev-middleware": "^1.10.2",
    "webpack-dev-server": "^2.4.5",
    "webpack-md5-hash": "^0.0.5",
    "webpack-merge": "^4.1.0"

A repro can be found here: dherges/ng-cli-lazy-loading-broken-6417. The change in package.json dependencies breaks the build.

The log given by the failure.

webpack: Compiling...
 40% building modules 1/2 modules 1 active ...ework\guide\app\app-routing.module.tsError: No module factory available for dependency type: ContextElementDependency
    at Compilation.addModuleDependencies (<project>\node_modules\@angular\cli\node_modules\webpack\lib\Compilation.js:206:21)
    at Compilation.processModuleDependencies (<project>\node_modules\@angular\cli\node_modules\webpack\lib\Compilation.js:195:8)
    at _this.buildModule.err (<project>\node_modules\@angular\cli\node_modules\webpack\lib\Compilation.js:336:13)
    at building.forEach.cb (<project>\node_modules\@angular\cli\node_modules\webpack\lib\Compilation.js:140:27)
    at Array.forEach (native)
    at callback (<project>\node_modules\@angular\cli\node_modules\webpack\lib\Compilation.js:140:13)
    at module.build (<project>\node_modules\@angular\cli\node_modules\webpack\lib\Compilation.js:167:11)
    at resolveDependencies (<project>\node_modules\@angular\cli\node_modules\webpack\lib\ContextModule.js:133:4)
    at ContextModule.result.resolveDependencies (<project>\node_modules\@ngtools\webpack\src\plugin.js:229:25)
    at ContextModule.build (<project>\node_modules\@angular\cli\node_modules\webpack\lib\ContextModule.js:103:8)
    at Compilation.buildModule (<project>\node_modules\@angular\cli\node_modules\webpack\lib\Compilation.js:142:10)
    at factoryCallback (<project>\node_modules\@angular\cli\node_modules\webpack\lib\Compilation.js:325:11)
    at <project>\node_modules\@angular\cli\node_modules\webpack\lib\ContextModuleFactory.js:96:12
    at <project>\node_modules\tapable\lib\Tapable.js:204:11
    at done.then (<project>\node_modules\@ngtools\webpack\src\plugin.js:231:28)
    at process._tickCallback (internal/process/next_tick.js:103:7)
 70% building modules 2/2 modules 0 active
 14% building modules 36/48 modules 12 active ...e_modules\rxjs\add\operator\filter.js<project>\node_modules\@angular\cli\node_modules\webpack\li
b\Compilation.js:265
                                if(_this.profile) {
                                        ^

TypeError: Cannot read property 'profile' of null
    at factoryCallback (<project>\node_modules\@angular\cli\node_modules\webpack\lib\Compilation.js:265:13)
    at factory (<project>\node_modules\@angular\cli\node_modules\webpack\lib\NormalModuleFactory.js:253:5)
    at applyPluginsAsyncWaterfall (<project>\node_modules\@angular\cli\node_modules\webpack\lib\NormalModuleFactory.js:99:14)
    at <project>\node_modules\tapable\lib\Tapable.js:204:11
    at NormalModuleFactory.params.normalModuleFactory.plugin (<project>\node_modules\@angular\cli\node_modules\webpack\lib\CompatibilityPlugin.js
:52:5)
    at NormalModuleFactory.applyPluginsAsyncWaterfall (<project>\node_modules\tapable\lib\Tapable.js:208:13)
    at resolver (<project>\node_modules\@angular\cli\node_modules\webpack\lib\NormalModuleFactory.js:74:11)
    at process.nextTick (<project>\node_modules\@angular\cli\node_modules\webpack\lib\NormalModuleFactory.js:205:8)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Desired functionality.

Lazy loading:

const routes: Routes = [
  {
    path: 'content',
    loadChildren: './+content/content.module#ContentModule'
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

Mention any other details that might be useful.

There are other related issues from the past #3793, #4246

dherges avatar May 23 '17 06:05 dherges

Closing as this seems to be caused by trying to install a different version of webpack than the one that @angular/cli depends on. This is not supported. Please ensure you only have @angular/cli in package.json, and not webpack. You can check if you have multiple versions of webpack by doing npm ls webpack.

filipesilva avatar Jul 17 '17 09:07 filipesilva

same issue here

npm ls -g --depth=0

/usr/lib
├── [email protected]
└── [email protected]

package.json:

  "dependencies": {
    "@angular/animations": "^4.3.0",
    "@angular/cdk": "github:angular/cdk-builds",
    "@angular/common": "^4.3.0",
    "@angular/compiler": "^4.3.0",
    "@angular/core": "^4.3.0",
    "@angular/flex-layout": "angular/flex-layout-builds",
    "@angular/forms": "^4.3.0",
    "@angular/http": "^4.3.0",
    "@angular/material": "angular/material2-builds",
    "@angular/platform-browser": "^4.3.0",
    "@angular/platform-browser-dynamic": "^4.3.0",
    "@angular/platform-server": "^4.3.0",
    "@angular/router": "^4.3.0",
    "angular2gridster": "^0.6.3",
    "core-js": "^2.4.1",
    "hammerjs": "^2.0.8",
    "rxjs": "^5.0.1",
    "zone.js": "^0.8.12"
  },
  "devDependencies": {
    "@angular/cli": "^1.2.3",
    "@angular/compiler-cli": "^4.3.0",
    "@types/jasmine": "2.5.46",
    "@types/node": "~7.0.12",
    "codelyzer": "~3.0.1",
    "simple-git": "^1.69.0",
    "simple-terminal-menu": "^1.1.3",
    "ts-node": "~3.0.4",
    "tslint": "^5.3.2",
    "typescript": "^2.4.1"
  }

ng serve

12% building modules 17/29 modules 12 active ...art/node_modules/zone.js/dist/zone.jsError: No module factory available for dependency type: ContextElementDependency
    at Compilation.addModuleDependencies (/home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/Compilation.js:206:21)
    at Compilation.processModuleDependencies (/home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/Compilation.js:195:8)
    at _this.buildModule.err (/home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/Compilation.js:336:13)
    at building.forEach.cb (/home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/Compilation.js:140:27)
    at Array.forEach (native)
    at callback (/home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/Compilation.js:140:13)
    at module.build (/home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/Compilation.js:167:11)
    at resolveDependencies (/home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/ContextModule.js:133:4)
    at ContextModule.result.resolveDependencies (/home/mike_oldfield/material2-start/node_modules/@ngtools/webpack/src/plugin.js:249:25)
    at ContextModule.build (/home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/ContextModule.js:103:8)
    at Compilation.buildModule (/home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/Compilation.js:142:10)
    at factoryCallback (/home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/Compilation.js:325:11)
    at /home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/ContextModuleFactory.js:96:12
    at /home/mike_oldfield/material2-start/node_modules/tapable/lib/Tapable.js:250:11
    at done.then (/home/mike_oldfield/material2-start/node_modules/@ngtools/webpack/src/plugin.js:251:28)               13% building modules 29/36 modules 7 active ...terial2-start/node_modules/rxjs/Rx.js/home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/Compilation.js:265
                                if(_this.profile) {
                                        ^

TypeError: Cannot read property 'profile' of null
    at factoryCallback (/home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/Compilation.js:265:13)
    at factory (/home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/NormalModuleFactory.js:253:5)
    at applyPluginsAsyncWaterfall (/home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/NormalModuleFactory.js:99:14)
    at /home/mike_oldfield/material2-start/node_modules/tapable/lib/Tapable.js:250:11
    at NormalModuleFactory.params.normalModuleFactory.plugin (/home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/CompatibilityPlugin.js:52:5)
    at NormalModuleFactory.applyPluginsAsyncWaterfall (/home/mike_oldfield/material2-start/node_modules/tapable/lib/Tapable.js:254:13)
    at resolver (/home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/NormalModuleFactory.js:74:11)
    at process.nextTick (/home/mike_oldfield/material2-start/node_modules/@angular/cli/node_modules/webpack/lib/NormalModuleFactory.js:205:8)
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)

source repository: https://github.com/rudzikdawid/material2-start

rudzikdawid avatar Jul 22 '17 07:07 rudzikdawid

I'm getting same error only on Teamcity build. Works fine locally.

When I remove lazy loaded routes then it works on Teamcity also.

dzolnjan avatar Jul 23 '17 18:07 dzolnjan

@rudzikdawid I checked your example and it definitely has two versions of webpack installed:

$ npm ls webpack
[email protected] D:\sandbox\material2-start
+-- @angular/[email protected]
| `-- [email protected]
`-- [email protected]
  `-- [email protected]

Refer back to my earlier comment (https://github.com/angular/angular-cli/issues/6417#issuecomment-315708400) about how this is not supported.

@dzolnjan if it works in one environment and not another, it's usually because your dependency set is differente. Maybe cache, maybe yarn.lock, maybe package-lock.json.

filipesilva avatar Jul 24 '17 10:07 filipesilva

Seems like this issue came back again

_                      _                 ____ _     ___

/ \ _ __ __ _ _ | | __ _ _ __ / | | | | / △ \ | ' \ / _ | | | | |/ _ | '| | | | | | | / ___ | | | | (| | || | | (| | | | || | | | // __| ||_, |_,||_,|| _||| |___/ @angular/cli: 1.4.4 node: 8.2.1 os: darwin x64 @angular/animations: 4.4.4 @angular/common: 4.4.4 @angular/compiler: 4.4.4 @angular/core: 4.4.4 @angular/forms: 4.4.4 @angular/http: 4.4.4 @angular/platform-browser: 4.4.4 @angular/platform-browser-dynamic: 4.4.4 @angular/router: 4.4.4 @angular/cli: 1.4.4 @angular/compiler-cli: 4.4.4 @angular/language-service: 4.4.4

Error: No module factory available for dependency type: ContextElementDependency

shahraship avatar Oct 04 '17 11:10 shahraship

I am getting this error as well npm ls webpack +-- @angular/[email protected] | -- [email protected] -- [email protected]

The error is No module factory available for dependency type: ContextElementDependency

Tried cleaning node modules and reinstalling, but the error persists when trying to use lazy loading

ng --version _ _ ____ _ ___ / \ _ __ __ _ _ | | __ _ _ __ / | | | | / △ \ | ' \ / _ | | | | |/ _ | '| | | | | | | / ___ | | | | (| | || | | (| | | | || | | | // __| ||_, |_,||_,|| _||| |___/ @angular/cli: 1.4.5 node: 8.4.0 os: win32 ia32 @angular/animations: 4.4.4 @angular/cdk: 2.0.0-beta.11 @angular/common: 4.4.4 @angular/compiler: 4.4.4 @angular/core: 4.4.4 @angular/forms: 4.4.4 @angular/http: 4.4.4 @angular/material: 2.0.0-beta.11 @angular/platform-browser: 4.4.4 @angular/platform-browser-dynamic: 4.4.4 @angular/router: 4.4.4 @angular/cli: 1.4.5 @angular/compiler-cli: 4.4.4 @angular/language-service: 4.4.4 typescript: 2.5.3

smasherprog avatar Oct 06 '17 18:10 smasherprog

As am I. I have confirmed that we only have a single Webpack version in our NPM and it is the one coming in with the Angular CLI.

Currently unable to do module lazy loading due to this issue.

webpack: 3.5.6 @angular/cli: 1.4.3 node: 8.1.0 @angular/core: 4.4.4 typescript: 2.3.0

NateWarner avatar Oct 06 '17 18:10 NateWarner

I did get this working

I deleted my package lock file delete node_modules remove any reference to webpack from my package.json npm install

smasherprog avatar Oct 06 '17 20:10 smasherprog

Doing as suggested by @smasherprog resolved my error and moved me forward with lazy load modules!

NateWarner avatar Oct 06 '17 22:10 NateWarner

@smasherprog Got me back on the right track. I wonder, does greenkeeper.io do this? It seems like we should be able to muddy up node_modules and then compare errors with Github questions so admins never have to deal with this again. @Realtin Maybe all major communities on Github such as Microsoft and Angular need to be using this?

ghost avatar Oct 16 '17 19:10 ghost

@megamindbrian if the newest webpack version update breaks the tests for angular-cli then yes, that is exactly what https://greenkeeper.io/ does. It keeps your build "green".

Greenkeeper creates a branch for a new dependency version and waits for the CI build status. If an in-range update of a dependency breaks your build (just like it happened here) it will create an issue and gives you the opportunity to create a pull request that would pin that dependency to the last working version.

This is how these issues look like: https://github.com/hoodiehq/hoodie/issues/776

Realtin avatar Oct 17 '17 15:10 Realtin

I did an ng eject a while back. Doing an ng new and comparing the changes from package.json and webpack.config.js solved my issues.

larssn avatar Oct 24 '17 09:10 larssn

Sorry this is a bit off topic, but I was just curious. @Realtin Is there a green-breaker? that proactively looks for dependency issues then files a known bug report for SEO for people looking for the same problem?

On Tue, Oct 24, 2017 at 2:34 AM, Lars Støttrup Nielsen < [email protected]> wrote:

I did an ng eject a while back. Doing an ng new and comparing the changes from package.json and webpack.config.js solved my issues.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/6417#issuecomment-338932241, or mute the thread https://github.com/notifications/unsubscribe-auth/AX5Xbpf-RQCcsfcndZXii-h6yOo6iUYvks5sva8ngaJpZM4NjN3M .

-- CONFIDENTIALITY NOTICE: The contents of this email message and any attachments are intended solely for the addressee(s) and may contain confidential and/or privileged information and may be legally protected from disclosure. It is then shared with tech companies, bots, hackers, government agencies, and marketers. The security of this message is none, and it may be shared on Instagram at anytime. If you are OK with this, please respond. There isn't really any security or privacy anywhere. If you disagree you may want to go camping and talk to people face-to-face like in old times.

ghost avatar Oct 27 '17 00:10 ghost

@filipesilva: Thank bro. This worked for me.

lamnv5490 avatar Oct 30 '17 16:10 lamnv5490

We're also struggling with this, and removing webpack from devDependencies doesn't seem to help. There's also no mismatch between the version under @angular/cli and the version referenced in package.json (which was inserted by ng eject):

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

Any ideas?

Quppa avatar Nov 28 '17 04:11 Quppa

Check out my post above... it will help

smasherprog avatar Nov 28 '17 04:11 smasherprog

@smasherprog As it turns out the webpack command is fine, so I can build the app - it's webpack-dev-server that's causing issues. The version under @angular/cli is 2.9.5, as is the version referenced in devDependencies.

~Update: Downgrading webpack-dev-server to ~2.7.1 - the version it was set to prior to my adventures in updating to Angular 5 - appears to have resolved the issue. I'm not thrilled with this solution, but at least I've got a working dev environment again.~

Update again: False alarm - I decided I'd try webpack-dev-server 2.8.x to see if 2.9.x was causing the issue, but now I've broken my environment again. Downgrading to 2.7.1 doesn't even help :(

Quppa avatar Nov 28 '17 05:11 Quppa

@Quppa any update since ?

stefdelec avatar Dec 04 '17 08:12 stefdelec

@stefdelec It's actually working right now, but I'm afraid that if I cleared node_modules and reinstalled everything it might break again. To be clear, webpack has been working reliably this whole time - for me it's been webpack-dev-server that has the problem. I'll continue my experiments later.

Quppa avatar Dec 04 '17 22:12 Quppa

I fixing following this steps:

  • npm uninstall --save-dev webpack
  • rm -rf node_modules
  • npm cache clean
  • Adding on devDependencies "@angular-devkit/core": "0.0.22", "@angular-devkit/schematics": "0.0.40"
  • npm install
    All work perfect now, my great problem was update @angular/: 5.0.0 => 5.0.3 😂

-- I using Node v8.9.3 with Webpack v3.10.0 and latest version of @angular/cli

kiramishima avatar Dec 13 '17 22:12 kiramishima