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.

Conditional Module imports (e.g. for development only)

See original GitHub issue

πŸš€ Conditional Module imports (e.g. for development-only modules)

Relevant Package

This feature request is for @angular/core / ivy

Description

View Engine allowed us to pass a module conditionally, which was useful as some modules should only be imported in development. Example:

@NgModule({
  imports: [
    process.env.NODE_ENV === 'development' ? NgxsReduxDevtoolsPluginModule.forRoot({})  : undefined
  ]
})

Using Ivy, the above solution will fail when building with production target. Running ng serve xxx --prod leads to these errors: CLI will show lots of misguiding errors (telling me component xxx is not a known element). Browser console will show β€œASSERTION ERROR: Type passed in is not NgModuleType, it does not have β€˜Ι΅mod’ property.”

I thought Ivy might not like the undefined value, so I tried this as a workaround:

@NgModule()
export class NoopModule {}

@NgModule({
  imports: [
    process.env.NODE_ENV === 'development' ? NgxsReduxDevtoolsPluginModule.forRoot({})  : NoopModule 
  ]
})

… which lead to the same errors.

Describe the solution you’d like

Does Ivy support this by now, in way I don’t know? if not, I suggest to allow to pass undefined to module imports.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
jbjhjmcommented, Jun 9, 2020

ok there is one important thing!!

One MUST NOT pass undefined into imports list under any circumstances.

This will fail during production build with Error Unexpected value 'undefined' imported by the module ...

imports: [
	environment.BUILD_MODE==='development' ? MyDevOnlyModule : undefined
],

But if you pass in an empty array as @alxhub does in his example above, it will work.

imports: [
	environment.BUILD_MODE==='development' ? MyDevOnlyModule : []
],

So examples and issues mentioned above might be due to dynamic environment. But the environment may also actually be analyzed correctly and the β€œundefined” value might be source of the problem.

1reaction
JoostKcommented, May 7, 2021

@LanderBeeuwsaert Angular 12 supports that particular use-case using the new BrowserAnimationsModule.withConfig API.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditional module imports in Angular | by Luuk Gruijs - Medium
Importing ngrx/store-devtools only in development can be done like this: ... I hope this little trick helps you to do conditional module imports....
Read more >
Conditional module imports in Angular - Agile Actors #learning
You want to use ngrx/store-devtools only in development and prevent it from being imported in production.
Read more >
How can I conditionally import an ES6 module? - Stack Overflow
You can now call the import keyword as a function (i.e. import() ) to load a module at runtime. It returns a Promise...
Read more >
Conditional module imports in Angular - LinkedIn
Importing ngrx/store-devtools only in development can be done like ... I hope this little trick helps you to do conditional module imports.
Read more >
Conditional module imports in Angular - HackerNoon
The best examples are: You're using ngrx/store and the ngrx/store-devtools module. You want to use ngrx/store-devtools only in development ...
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