You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NestJS微服务中DeprecationWarning警告消除方法咨询

NestJS gRPC微服务消除start()废弃警告的方案

控制台持续输出以下废弃警告:

(node:6600) DeprecationWarning: Calling start() is no longer necessary. It can be safely omitted. (Use node --trace-deprecation ... to show where the warning was created)

当前使用的main.ts代码:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { resolve } from 'path';
import { USERS_PACKAGE_NAME } from '@globals/interfaces/users.proto';

async function bootstrap() {
    const app = await NestFactory.createMicroservice<MicroserviceOptions>(
      AppModule,
      {
        transport: Transport.GRPC,
        options: {
          url: 'localhost:50050',
          package: USERS_PACKAGE_NAME,
          protoPath: resolve(__dirname, '../../globals/protos/users.proto'),
        },
      },
    );
    await app.listen();
}
bootstrap();

依赖版本:

@grpc/grpc-js: ^1.10.7
@nestjs/microservices: ^10.3.8
nest: 10.3.0
node: v20.11.0

曾尝试用app.init()替代app.listen(),但替换后服务器无法接收任何请求或连接。


解决方案

1. 保留app.listen(),不要替换为app.init()

app.init()仅完成应用模块和DI容器的初始化,不会启动gRPC服务的监听端口,因此必须保留await app.listen()才能让服务正常接收请求。

2. 消除警告的可行方法

方法一:升级依赖版本

该警告源于@grpc/grpc-js标记start()方法为废弃,而旧版@nestjs/microservices内部仍调用了该方法。尝试升级@nestjs/microservices到最新稳定版,新版本通常会适配@grpc/grpc-js的废弃变更,移除对start()的调用。

方法二:精准过滤特定警告

在main.ts最顶部添加以下代码,拦截并忽略该特定废弃警告,同时保留其他警告信息:

process.on('warning', (warning) => {
  if (warning.message.includes('Calling start() is no longer necessary')) {
    return;
  }
  console.warn(warning);
});

方法三:通过启动命令忽略警告

修改项目package.json中的启动脚本,添加Node.js参数忽略废弃警告:

"scripts": {
  "start": "node --no-warnings=DeprecationWarning dist/main.js"
}

如果需要全局忽略所有废弃警告(不推荐),可使用:

"scripts": {
  "start": "node --no-deprecation dist/main.js"
}

内容的提问来源于stack exchange,提问作者Abduhodi Tursunboyev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 16:52:38