Skip to content

AoT Compiler unable to resolve module with "export default class" #11402

Closed

Description

@RoxKilly

I'm submitting a ...

[x] bug report => search github for a similar issue or PR before submitting

Current behavior
To allow a module to be loaded lazily, I define its route like so:

{
    path: 'report',
    loadChildren: '/app/report/report.module'
}

And I declare it as the default class in report.module.ts so the router will know what to load:

export default class ReportModule {}

Using export default class ReportModule {} instead of export class ReportModule {} when declaring modules breaks ngc. During compilation I get error:

can't resolve module C:/path/to/report.module.ts from C:/path/to/report.module.ts
Error: can't find symbol ReportModule exported from module C:/path/to/report.module.ts
    at ReflectorHost.findDeclaration (C:\path\to\@angular\compiler-cli\src\reflector_host.js:174:23)
    at _loop_1 (C:\path\to\@angular\compiler-cli\src\codegen.js:52:51)
    ...
Compilation failed

I left the orientation of the slashes (forward / and back \) as they appear in the console

Expected/desired behavior

An earlier version of the docs on ngModule suggested using export default to expose lazy-loaded modules. This works well with Just-in-Time compilation. So I expected the AoT compiler to be able to parse them as well

Reproduction of the problem

Try compiling an application with a module declared with export default

Please tell us about your environment:

  • Angular version: 2.0.0-rc.6
  • Browser: [all]
  • Language: [TypeScript 2]

Activity

  1. vicb commented on Sep 7, 2016

    @vicb
    Contributor
  2. vicb commented on Sep 8, 2016

    @vicb
    Contributor

    @mhevery @alexeagle Chuck says this is related to your module resolution changes.

  3. changed the title [-]AoT Compiler unable to parse valid typescript: "export default class"[/-] [+]AoT Compiler unable to resolve module with "export default class"[/+] on Sep 8, 2016
  4. mhevery commented on Sep 10, 2016

    @mhevery
    Contributor

    @vsavkin it seems like the Router does not understand default exports?

  5. alexeagle commented on Sep 11, 2016

    @alexeagle
    Contributor

    No, this is in ReflectorHost so I don't think it's router-specific.

  6. robwormald commented on Sep 11, 2016

    @robwormald
    Contributor

    I think the compiler / codgen pipeline in general doesn't handle default exports (yet)

  7. 34 remaining items

  8. spinspume commented on May 29, 2019

    @spinspume

    This is still a thing, error message could be better:
    'ERROR in : Unexpected value 'null' imported by the module' when you export a routing module as default.

    If I hadn't found a thread referencing the same error this would've been a pain to solve.

  9. SchnWalter commented on Sep 27, 2019

    @SchnWalter
    Contributor

    Haven't checked, but I just saw commint two commits that landed into Angular v9 and I think they might also solve this issue, see: 373e133 and c4e039a

  10. Ni55aN commented on Nov 12, 2019

    @Ni55aN

    Wow, 2019 is coming to an end, the problem still exist

  11. removed their assignment
    on Nov 15, 2019
  12. montella1507 commented on Nov 26, 2019

    @montella1507

    Wow, 2019 is coming to an end, the problem still exist

    same for i18n and working solution

  13. niklaas commented on Jan 24, 2020

    @niklaas

    This can be fixed by providing the exported class through a factory provider. The following is an example providing MarkerClusterer from @google/markerclustererplus:

    import MarkerClusterer from '@google/markerclustererplus';
    
    export const MARKER_CLUSTERER_CLASS = new InjectionToken<new (...args: any) => MarkerClusterer>(
      'MarkerClustererPlus class from @google/markerclustererplus',
    );
    
    export function markerClustererClassFactory(): new (...args: any) => MarkerClusterer {
      return MarkerClusterer;
    }
    
    @NgModule({
      providers: [
        { provide: MARKER_CLUSTERER_CLASS, useFactory: markerClustererClassFactory },
      ],
    })
    export class SomeModule {}
    
    @Injectable()
    export class SomeService {
      constructor(@Inject(MARKER_CLUSTERER_CLASS) private markerClustererClass) {}
    }
    ❯ ng --version
    
         _                      _                 ____ _     ___
        / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
       / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
      / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
     /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                    |___/
    
    
    Angular CLI: 8.3.23
    Node: 10.16.1
    OS: linux x64
    Angular: 8.2.14
    ... animations, common, compiler, compiler-cli, core, forms
    ... language-service, platform-browser, platform-browser-dynamic
    ... router
    
    Package                            Version
    ------------------------------------------------------------
    @angular-devkit/architect          0.803.23
    @angular-devkit/build-angular      0.803.23
    @angular-devkit/build-ng-packagr   0.803.23
    @angular-devkit/build-optimizer    0.803.23
    @angular-devkit/build-webpack      0.803.23
    @angular-devkit/core               8.3.23
    @angular-devkit/schematics         8.3.23
    @angular/cli                       8.3.23
    @ngtools/webpack                   8.3.23
    @schematics/angular                8.3.23
    @schematics/update                 0.803.23
    ng-packagr                         5.7.1
    rxjs                               6.5.4
    typescript                         3.5.3
    webpack                            4.39.2
    
  14. added
    area: compilerIssues related to `ngc`, Angular's template compiler
    and removed
    area: coreIssues related to the framework runtime
    on Mar 8, 2020
  15. JoostK commented on Mar 15, 2020

    @JoostK
    Member

    Hey folks,

    default exports are now working in the Ivy compiler!

  16. angular-automatic-lock-bot commented on Apr 15, 2020

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  17. locked and limited conversation to collaborators on Apr 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions