question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

webpack2: Cannot read property 'forRoot' of undefined

See original GitHub issue

Bug, feature request, or proposal:

After building the app with webapck, I get the following error in browser console:

Uncaught TypeError: Cannot read property 'forRoot' of undefined
    at app.bundle.js:76094
    at Object.<anonymous> (app.bundle.js:76119)
    at __webpack_require__ (app.bundle.js:48)
    at Object.<anonymous> (app.bundle.js:115880)
    at __webpack_require__ (app.bundle.js:48)
    at app.bundle.js:138
    at app.bundle.js:141

And the following is the bundle (line 76094 is the last one):

var AppModule = (function () {
    function AppModule() {
    }
    AppModule = __decorate([
        __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__angular_core__["b" /* NgModule */])({
            imports: [
                __WEBPACK_IMPORTED_MODULE_1__angular_platform_browser__["a" /* BrowserModule */],
                __WEBPACK_IMPORTED_MODULE_3__app_routes__["a" /* routing */],
                __WEBPACK_IMPORTED_MODULE_7__shared_shared_module__["a" /* SharedModule */],
                __WEBPACK_IMPORTED_MODULE_8__angular_material__["MaterialModule"].forRoot(),
...

What are the steps to reproduce?

import { MaterialModule } from '@angular/material';

@NgModule({
    imports: [
        BrowserModule,
        ...
        MaterialModule.forRoot()
    ],
    declarations: [
        AppComponent,
        ...
    ],
    providers: [
    ],
    bootstrap: [AppComponent]
})

export class AppModule { }

Which versions of Angular, Material, OS, browsers are affected?

Angular 2.4.0 Angular Material 2.0.0-beta.1 webpack 2.2.0-rc.1 Node v6.2.2 npm 4.0.2

Is there anything else we should know?

I removed the precedent version of Material before installing the new version. No problem up to version 2.0.0-alpha.11-3

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
devversioncommented, Dec 26, 2016

@robisim74 Uff, it took me a while to figure out what causes the issues with Webpack 2.

The problem is that SHA https://github.com/angular/material2/commit/28691ca78042aed428781a3d88af7fc5cff0f8bb introduced a new file, which only includes TypeScript typings.

This means that the file is completely empty when compiled to JavaScript.

Now the Material 2 core package tries to re-export all export’s from this file… but there are no exports and Webpack 2 is now somehow failing to parse following exports from other files.

This means that the MaterialModule will be undefined and your Angular 2 application doesn’t work.

I just filed an issue on the Webpack repository. See https://github.com/webpack/webpack/issues/3584

@jelbourn As a temporary fix, I’d recommend moving the annotations to the gesture-config file, because those always have one export.

3reactions
robisim74commented, Dec 25, 2016

Thanks @tuurbo, I will try it. But I think that the import from “@angular/material” should work without any workaround, as “@angular/core” and all the other packages.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RouterModule : Cannot read property 'forRoot' of undefined
So I used RouterModule to read query params. But I am getting error. Cannot read property 'forRoot' of undefined.
Read more >
Karma tests with modal problem - Material Design for Bootstrap
Failed: Template parse errors: There is no directive with "exportAs" set to "mdb-modal" ... Can't bind to 'config' since it isn't a known...
Read more >
uncaught typeerror: cannot read properties of undefined ...
I just added preact/hook import on a file. I have already tried to see if I was calling render more times, or id...
Read more >
48 answers on StackOverflow to the most popular Angular ...
Angular exception: Can't bind to 'ngForIn' since it isn't a known native ... EXCEPTION: TypeError: Cannot read property 'name' of null in ...
Read more >
ngx-webstorage - npm
The forRoot is now mandatory in the root module even if you don't need ... caseSensitive:true }) // The forRoot method allows to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found