Open arvinsingla opened 4 years ago
got same problem, seams ConfigModule.load is not reached
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 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 I'm currently using 13.6.0. Can you share with me your setup?
@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.
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.
hmmm ok, thanks guys @arvinsingla @alexk4. I'll take a look into it. Strange it's this version only.
@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
What OS are you running?
@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.
npm run build
docker image build -t nestjs-config-node-lts .
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.
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.
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
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!
1.4.6 has been released wih @romamd changes! Thanks for taking the time and solving the issue!
Unfortunately, the problem still persist with 1.4.6.
@mirono It works for me with node v12.16.1. What node version do you have?
On production I have 10.19.0. On dev 10.0.0. I'll check if I can upgrade.
Still doesn't work with v12.16.2.
@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
Is there a working example that would work also in production (i.e. using the dist folder)?
I had this problem. I did downgrade 12.16 to 12.13, it works
This fixed it for me
const config = app.get<ConfigService>(ConfigService);
const secret = config['env'].EXAMPLE_SECRET;
Let me know if this helped 🤘
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 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 this works also with Nest 6.
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.6I'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:
Is there any solution to this. I am still having this issue.
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.6I'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))
you the man @MGough !
add this to your "app.module.ts" in the imports array:
ConfigModule.forRoot({ isGlobal: true }),
Issue type:
nestjs-config version
v1.4.5
@nestjs/common+core or other package versions
Excepted behavior
ConfigService
as available inmain.ts
by leveragingapp.get('ConfigService')
Actual behavior or outcome (for issue)
Application fails to start with the following error.
Replication/Example
Works perfectly fine for all node LTS versions prior to 12.16.0.
app.module.ts
main.ts