nestjs-config: Unable to app.get(ConfigService) in main.ts with node lts (12.16.0)

Issue type:

  • question
  • bug report
  • feature request
  • documentation issue

nestjs-config version v1.4.5

@nestjs/common+core or other package versions

├─┬ @nestjs/common@6.11.7
├─┬ @nestjs/core@6.11.7
├─┬ @nestjs/microservices@6.11.7
├─┬ @nestjs/platform-express@6.11.7
├─┬ @nestjs/swagger@3.1.0
├─┬ @nestjs/testing@6.11.7
├─┬ @nestjs/typeorm@6.3.1

Excepted behavior

ConfigService as available in main.ts by leveraging app.get('ConfigService')

Actual behavior or outcome (for issue)

Application fails to start with the following error.

[Nest] 80230   - 2020-02-18, 1:58:04 p.m.   [ExceptionHandler] Nest could not find ConfigService element (this provider does not exist in the current context)
Error: Nest could not find ConfigService element (this provider does not exist in the current context)
[0]     at ContainerScanner.getWrapperCollectionPairByHost (/Users/arvins/Sites/service/node_modules/@nestjs/core/injector/container-scanner.js:34:19)
[0]     at ContainerScanner.findInstanceByToken (/Users/arvins/Sites/service/node_modules/@nestjs/core/injector/container-scanner.js:20:40)
[0]     at ContainerScanner.find (/Users/arvins/Sites/service/node_modules/@nestjs/core/injector/container-scanner.js:13:21)
[0]     at NestApplication.find (/Users/arvins/Sites/service/node_modules/@nestjs/core/nest-application-context.js:197:38)
[0]     at NestApplication.get (/Users/arvins/Sites/service/node_modules/@nestjs/core/nest-application-context.js:43:25)
[0]     at /Users/arvins/Sites/service/node_modules/@nestjs/core/nest-factory.js:111:40
[0]     at Function.run (/Users/arvins/Sites/service/node_modules/@nestjs/core/errors/exceptions-zone.js:8:13)
[0]     at Proxy.<anonymous> (/Users/arvins/Sites/service/node_modules/@nestjs/core/nest-factory.js:110:46)
[0]     at /Users/arvins/Sites/service/dist/src/main.js:59:30
[0]     at Generator.next (<anonymous>)

Replication/Example

Works perfectly fine for all node LTS versions prior to 12.16.0.

app.module.ts

import { Module } from '@nestjs/common'
import { ConfigModule, ConfigService } from 'nestjs-config'
import { HealthModule } from './health/health.module'
import * as path from 'path'

@Module({
	imports: [
		HealthModule,
		ConfigModule.load(path.resolve(__dirname, 'config', '**/!(*.d).{ts,js}')),
	],
})
export class AppModule {}

main.ts

async function bootstrap() {
  const app = await NestFactory.create(AppModule)
  const config = app.get('ConfigService')
  await app.listen(config.get('express.port'))
}
bootstrap()

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 8
  • Comments: 30 (2 by maintainers)

Commits related to this issue

Most upvoted comments

I had the same problem and solved it by first importing the ConfigModule in app.module.ts like mentioned in the documentation.

After that, I could use it in the main.ts without problems (example main.ts):

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestExpressApplication } from '@nestjs/platform-express';

async function bootstrap() {
  const logger = new Logger('Bootstrapper');

  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  const config = app.get<ConfigService>(ConfigService);

  const port = config.get('API_PORT');
  await app.listen(Number(port));

  logger.log(`App listening on port ${port}`);
}
bootstrap();

I am currently having this issue when I upgrade the version of my NestJS packages to v8, I was trying to implement versioning so I have to upgrade to v8. But this error is happening when calling config service in main.ts with node v14.7.6

I’m not actually sure if I’m in the right repo here, but I ended up here and may have a solution…

In main.ts async function bootstrap() I was doing this before NestJS v8:

  const configService = app.get<ConfigService>("ConfigService");

I had to change it to:

  const configService = app.get<ConfigService>(ConfigService);

add this to your “app.module.ts” in the imports array:

ConfigModule.forRoot({ isGlobal: true }),

This fixed it for me

const config = app.get<ConfigService>(ConfigService);
const secret = config['env'].EXAMPLE_SECRET;

Let me know if this helped 🤘

Have you tried awaiting the container before the get? Like so?

const app = await NestFactory.create(AppModule);
await app.init();
const config = app.get(ConfigService);

I am currently having this issue when I upgrade the version of my NestJS packages to v8, I was trying to implement versioning so I have to upgrade to v8. But this error is happening when calling config service in main.ts with node v14.7.6

I had this problem. I did downgrade 12.16 to 12.13, it works

1.4.6 has been released wih @romamd changes! Thanks for taking the time and solving the issue!

@arvinsingla ok so I cloned your repo, installed with npm, ran node with version 12.6.0, used both npm run start and npm run start:dev and this was my result Screenshot 2020-02-20 at 08 58 50 What OS are you running?