nestjs-config icon indicating copy to clipboard operation
nestjs-config copied to clipboard

Unable to app.get(ConfigService) in main.ts with node lts (12.16.0)

Open arvinsingla opened this issue 5 years ago • 30 comments

Issue type:

  • [ ] question
  • [x] bug report
  • [ ] feature request
  • [ ] documentation issue

nestjs-config version v1.4.5

@nestjs/common+core or other package versions

├─┬ @nestjs/[email protected]
├─┬ @nestjs/[email protected]
├─┬ @nestjs/[email protected]
├─┬ @nestjs/[email protected]
├─┬ @nestjs/[email protected]
├─┬ @nestjs/[email protected]
├─┬ @nestjs/[email protected]

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()

arvinsingla avatar Feb 18 '20 19:02 arvinsingla

got same problem, seams ConfigModule.load is not reached

dmitrirussu avatar Feb 19 '20 09:02 dmitrirussu

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);

bashleigh avatar Feb 19 '20 09:02 bashleigh

@bashleigh ConfigService load works fine for node version <=12.13.0... for the upper versions does not work... and reason is not in awaiting for.... in my case I'm waiting... no any config provided... Missing ConfigService Module provider...

dmitrirussu avatar Feb 19 '20 10:02 dmitrirussu

@dmitrirussu I'm currently using 13.6.0. Can you share with me your setup?

bashleigh avatar Feb 19 '20 12:02 bashleigh

@bashleigh Unfortunately await app.init() did not help. The example I put in the description is a toy example. We've got three nest services in production and this issue first started rearing it's head in CI last week coinciding with the release of node 12.6.0 LTS. After a bunch of investigation the only way the issue was mitigated was by reverting back to node 12.5.0.

I put together a very simple repo with a clean install of nest using nest-cli and nestjs-config as the only dependency. The issue is present on all node versions I have tested > 12.6.0 including 13.x.

https://github.com/arvinsingla/nestjs-config-node-lts/

arvinsingla avatar Feb 19 '20 13:02 arvinsingla

I'm experiencing the same issue with Node 12.6.1 and NestJS 6.11.x. Nest could not find ConfigService element (this provider does not exist in the current context) No issues with Node 12.14.0.

alexk4 avatar Feb 19 '20 16:02 alexk4

hmmm ok, thanks guys @arvinsingla @alexk4. I'll take a look into it. Strange it's this version only.

bashleigh avatar Feb 20 '20 08:02 bashleigh

@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?

bashleigh avatar Feb 20 '20 09:02 bashleigh

@bashleigh I'm running macOS Mojave, but we're also experiencing the issue in CI where the application is run in Linux docker containers.

I've added a dockerfile to my sample repo if you want to try seeing if you can reproduce.

  1. npm run build
  2. docker image build -t nestjs-config-node-lts .
  3. docker container run --publish 3000:3000 nestjs-config-node-lts

If you aren't able to reproduce my suspicion is there must be something happening at the build level that's different on your machine than ours.

arvinsingla avatar Feb 21 '20 13:02 arvinsingla

The only difference I could find was with nest. Globally I have nest 6.6.3, in your project you have 6.14.2. However! Running node_modules/.bin/nest start --watch it works locally! I tried running node_modules/.bin/ts-node src/main.ts from the container but I couldn't find ./node_modules. I'll have another try at seeing what the issue is. I thought I'd just let you know my findings thus far.

bashleigh avatar Feb 24 '20 14:02 bashleigh

I also have the same issue:

const app = await NestFactory.create<NestExpressApplication>(AppModule);
await app.init();
const configService = app.get(ConfigService);
console.log(configService);
const port = configService.get('app.config').port;
console.log('Binding server to port ' + port);
ConfigService { internalConfig: {} }
(node:61637) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'port' of undefined
    at bootstrap (/Users/mo39119/Dev/Familia/Server/node/nest-api/api/dist/main.js:13:49)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:61637) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:61637) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.```

Node v10.15.3 on MacOS

mirono avatar Mar 03 '20 16:03 mirono

Sorry for taking ages guys. I've just resigned from the stressful job that I had so I now have all the time in the world! Will release the fix now!

bashleigh avatar Apr 03 '20 11:04 bashleigh

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

bashleigh avatar Apr 03 '20 11:04 bashleigh

Unfortunately, the problem still persist with 1.4.6.

mirono avatar Apr 10 '20 17:04 mirono

@mirono It works for me with node v12.16.1. What node version do you have?

romamd avatar Apr 10 '20 20:04 romamd

On production I have 10.19.0. On dev 10.0.0. I'll check if I can upgrade.

mirono avatar Apr 11 '20 08:04 mirono

Still doesn't work with v12.16.2.

mirono avatar Apr 11 '20 08:04 mirono

@mirono

On production I have 10.19.0. On dev 10.0.0. I'll check if I can upgrade.

It's probably some other issue, because this one is reproducing only on node >= 12.16.0

romamd avatar Apr 11 '20 10:04 romamd

Is there a working example that would work also in production (i.e. using the dist folder)?

mirono avatar Apr 11 '20 10:04 mirono

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

dcyrillo avatar Apr 24 '20 05:04 dcyrillo

This fixed it for me

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

Let me know if this helped 🤘

FrencisAlreadyInUse avatar Feb 25 '21 09:02 FrencisAlreadyInUse

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();

H256 avatar Mar 07 '21 17:03 H256

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

rteano avatar Sep 10 '21 08:09 rteano

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);

MGough avatar Sep 20 '21 11:09 MGough

@MGough this works also with Nest 6.

mirono avatar Sep 20 '21 17:09 mirono

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);

Great catch! it worked, guess, they should improved with the typings for this one. :+1:

rteano avatar Sep 22 '21 06:09 rteano

Is there any solution to this. I am still having this issue.

hnviradiya avatar Jan 07 '22 19:01 hnviradiya

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);

THANKSSS, worked for me too! (I had this error after upgrading @nestjs/core, @nestjs/common, ... to latest version (in case 8.2.6))

jerembdn avatar Feb 08 '22 18:02 jerembdn

you the man @MGough !

anirudhsaligram avatar May 26 '22 09:05 anirudhsaligram

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

ConfigModule.forRoot({ isGlobal: true }),

amir-khoshbakht avatar Nov 17 '22 14:11 amir-khoshbakht