“ERROR in window is not defined” webpack general error on webpack --mode=production --env.prod

后端 未结 1 1159
迷失自我
迷失自我 2020-12-19 22:15


I\'m trying to deploy my first angular app but during the execution of deploy, in the middle of the command:

node node_modules/webpack/bin/webpack.js --         


        
相关标签:
1条回答
  • 2020-12-19 22:37

    This is probably cause because you're referring to window directly somewhere in your code. THIS IS NOT BEST PRACTISE!! Sometimes, (Server side) the window object is not defined and cause this problems. Same if a third-part library uses that.

    So check your code first, find where you're using window directly and wrap inside a PLATFORMID like this example:

     import { PLATFORM_ID } from '@angular/core';
     import { isPlatformBrowser, isPlatformServer } from '@angular/common';
     
     constructor(@Inject(PLATFORM_ID) private platformId: Object) { ... }
     
     ngOnInit() {
       if (isPlatformBrowser(this.platformId)) {
          // Client only code.
          ...
       }
       if (isPlatformServer(this.platformId)) {
         // Server only code.
         ...
       }
     }

    Or, if you can't find it, probably a library is still using it. Check this issue on github, it will explain you better than me the problem:

    https://github.com/webpack/react-starter/issues/37

    Hope this will help you =)

    PS: same problem when i had to implement a Server Side Rendering.. I was using window EVERYWHERE in code. From that moment, when i have to use it, i always find another way to do the same thing.

    You could also try putting target: "web" inside your webpack.config.js


    EDIT FROM THE ONE WHO ASKED THE QUESTION

    See also this issue on github, it provides the solution for this issue: https://github.com/webpack/webpack/issues/7112

    0 讨论(0)
提交回复
热议问题