Skip to content

6.0.0 build fails with lazy modules and Node API #10698

@Enngage

Description

@Enngage

Versions

Angular CLI: 6.0.0
Node: 8.9.4
OS: win32 x64
Angular: 6.0.0
... animations, cli, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.6.0
@angular-devkit/build-angular     0.6.0
@angular-devkit/build-optimizer   0.6.0
@angular-devkit/core              0.6.0
@angular-devkit/schematics        0.6.0
@ngtools/webpack                  6.0.0
@schematics/angular               0.6.0
@schematics/update                0.6.0
rxjs                              6.1.0
typescript                        2.7.2
webpack                           4.6.0

Observed behavior

We have application that lazy loads module that depends on node Api and is included only if process is running under node using:

 if (typeof process !== 'undefined' && Object.prototype.toString.call(process) === '[object process]') {
      // require module with Node Api
      const adapter = require('./moduleWithNodeApi');    
}

We confirmed that this branch is not hit when building the Angular application and its also not hit when running ng serve. For some reasons, however, the builder seems to be attempting to resolve the module anyway. Because of that we get this error during compilation:

ERROR in ./node_modules/parse5/lib/parser/parser_stream.js
Module not found: Error: Can't resolve 'stream' in 'c:\dev\parse5test\node_modules\parse5\lib\parser'
ERROR in ./node_modules/parse5/lib/sax/index.js
Module not found: Error: Can't resolve 'stream' in 'c:\dev\parse5test\node_modules\parse5\lib\sax'
ERROR in ./node_modules/parse5/lib/sax/dev_null_stream.js
Module not found: Error: Can't resolve 'stream' in 'c:\dev\parse5test\node_modules\parse5\lib\sax'
ERROR in ./node_modules/parse5/lib/serializer/serializer_stream.js
Module not found: Error: Can't resolve 'stream' in 'c:\dev\parse5test\node_modules\parse5\lib\serializer'

Sure, we could use parse5 on version 4 (which does have 'sort of' workaround for running under node), but then we get a warning message like:

WARNING in ./node_modules/parse5/lib/index.js
55:23-49 Critical dependency: the request of a dependency is an expression

We know we can skip this in webpack.config (and we did), but it seems to not be relevant to the issue I'm raising. I would expect that lazy modules would not be resolved during compilation. They can fail during runtime, thats fine (though it should not happen because we include different modules in different environments).

This worked perfectly fine with angular/cli@1.7.3

Desired behavior

Don't resolve lazy loaded modules with require when building an app because these modules might contain API that does not exist in given environment.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions