#Optional module in dev only?

2 messages · Page 1 of 1 (latest)

true vigil
#

I installed @nestjs/devtools-integration as a dev dependency. Now in production, I get the following error:

Cannot find module '@nestjs/devtools-integration'
// app.module.ts
import { Module } from '@nestjs/common';
import { DevtoolsModule } from '@nestjs/devtools-integration';

@Module({
  imports: [DevtoolsModule.register()],
})
export class AppModule {}

Is there a way to mark it non-required based on NODE_ENV, and get past this error?

Tried the following but it doesn't work:

@Module({
  imports: [
    ...(process.env.NODE_ENV !== 'production'
      ? []
      : [
          import('@nestjs/devtools-integration').then((devtools) => {
            return devtools.DevtoolsModule.register({
              http: process.env.NODE_ENV !== 'production',
            });
          }),
        ]),
  ],
})
export class AppModule {}

Also tried to catch the error with process.on('unhandledRejection', (error) => {}) but no luck.

The docs don't mention optional modules, is that not possible? What's the recommended approach here?

harsh cove
#

The second approach is almost there. It does not work, because you're passing a promise as the module, not the module itself. But you can use require instead, which is sync