Search code examples
nestjsdotenvnestjs-config

NestJS App won't use the .env or configService PORT. Always starts on port 3000 no matter what


I have a barebones NestJS app where all I have done is add a .env file with PORT=3001 as the content and then modified my main.ts according to the NestJS docs:

import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule, {bufferLogs: true});
  const configService = app.get(ConfigService);
  const PORT = configService.get('PORT');
  app.listen(PORT);
}
bootstrap();

My AppModule:

@Module({
  imports: [
    ConfigModule.forRoot({isGlobal: true}),
    UsersModule
  ],
  controllers: [AppController],
  providers: [AppService],
})

export class AppModule {}

When I run the app, it always runs on port 3000. It never runs on port 3001. What is going on???


Solution

  • So it turns out that npm run start:dev (aka "start:dev": "nest start --watch") doesn't actually rebuild! I had to kill the process, run npm run build to update my dist folder, and then run npm run start:dev. What a headache.