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
- Fix issue #275: Unable to app.get(ConfigService) in main.ts with node lts (12.16.0) — committed to romamd/nestjs-config by romamd 4 years ago
- Fix issue #275: Unable to app.get(ConfigService) in main.ts with node lts (12.16.0) (remove unused dependency) — committed to romamd/nestjs-config by romamd 4 years ago
- Merge pull request #291 from romamd/fix-issue-275 Fix issue #275: Unable to app.get(ConfigService) in main.ts with node 12.16 — committed to nestjsx/nestjs-config by bashleigh 4 years ago
- -Moved main.ts to root level so that both by command npm run and vs code can run same code -ModuleResolution node is causing issue to resolve modules. So removed from tsconfig. -configService is not w... — committed to hnviradiya/contact-list by hnviradiya 2 years ago
I had the same problem and solved it by first importing the ConfigModule in
app.module.tslike mentioned in the documentation.After that, I could use it in the main.ts without problems (example
main.ts):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.tsasync function bootstrap()I was doing this before NestJS v8:I had to change it to:
add this to your “app.module.ts” in the imports array:
ConfigModule.forRoot({ isGlobal: true }),This fixed it for me
Let me know if this helped 🤘
Have you tried awaiting the container before the get? Like so?
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.tswith node v14.7.6I 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
What OS are you running?
npm run startandnpm run start:devand this was my result