Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 of 4 tasks
arvinsingla opened this issue Feb 18, 2020 · 30 comments
Open
1 of 4 tasks

Comments

@arvinsingla
Copy link

Issue type:

  • question
  • 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()
@dmitrirussu
Copy link

got same problem, seams ConfigModule.load is not reached

@bashleigh
Copy link
Collaborator

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

@dmitrirussu
Copy link

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

@bashleigh
Copy link
Collaborator

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

@arvinsingla
Copy link
Author

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

@alexk4
Copy link

alexk4 commented Feb 19, 2020

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.

@bashleigh
Copy link
Collaborator

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

@bashleigh
Copy link
Collaborator

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

@arvinsingla
Copy link
Author

arvinsingla commented Feb 21, 2020

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

@bashleigh
Copy link
Collaborator

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.

@mirono
Copy link

mirono commented Mar 3, 2020

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

romamd added a commit to romamd/nestjs-config that referenced this issue Mar 16, 2020
romamd added a commit to romamd/nestjs-config that referenced this issue Mar 16, 2020
…th node lts (12.16.0) (remove unused dependency)
bashleigh added a commit that referenced this issue Apr 3, 2020
Fix issue #275: Unable to app.get(ConfigService) in main.ts with node 12.16
@bashleigh
Copy link
Collaborator

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
Copy link
Collaborator

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

@mirono
Copy link

mirono commented Apr 10, 2020

Unfortunately, the problem still persist with 1.4.6.

@romamd
Copy link
Contributor

romamd commented Apr 10, 2020

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

@mirono
Copy link

mirono commented Apr 11, 2020

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

@mirono
Copy link

mirono commented Apr 11, 2020

Still doesn't work with v12.16.2.

@romamd
Copy link
Contributor

romamd commented Apr 11, 2020

@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

@mirono
Copy link

mirono commented Apr 11, 2020

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

@dcyrillo
Copy link

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

@FrencisAlreadyInUse
Copy link

This fixed it for me

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

Let me know if this helped 🤘

@H256
Copy link

H256 commented Mar 7, 2021

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

@rteano
Copy link

rteano commented Sep 10, 2021

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

@MGough
Copy link

MGough commented Sep 20, 2021

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

@mirono
Copy link

mirono commented Sep 20, 2021

@MGough this works also with Nest 6.

@rteano
Copy link

rteano commented Sep 22, 2021

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. 👍

@hnviradiya
Copy link

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

hnviradiya added a commit to hnviradiya/contact-list that referenced this issue Jan 7, 2022
…ode can run same code

-ModuleResolution node is causing issue to resolve modules. So removed from tsconfig.
-configService is not working in main.ts. Bug is already present on github: nestjsx/nestjs-config/issues/275
-as configService is having issue, resolved config using json file.
@jerembdn
Copy link

jerembdn commented Feb 8, 2022

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

@anirudhsaligram
Copy link

you the man @MGough !

@amir-khoshbakht
Copy link

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

ConfigModule.forRoot({ isGlobal: true }),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests