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

运行Docker时如何隐藏Node.js弃用警告?已尝试--quiet无效

问题描述

构建并运行Docker镜像时,持续收到webpack-dev-server的废弃警告,尝试使用--quiet参数静默警告无效。

警告信息

(node:26) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE]
DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated.
Please use the 'setupMiddlewares' option. (Use `node --trace-deprecation ...` to show where the warning was created)
(node:26) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE]
DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated.
Please use the 'setupMiddlewares' option. Starting the development server...

构建日志

➜  ace git:(main) ✗ docker build -t ace-image .      
[+] Building 9.7s (11/11) FINISHED                                                                                         
 => [internal] load build definition from Dockerfile                                                                  0.0s
 => => transferring dockerfile: 625B                                                                                  0.0s
 => [internal] load .dockerignore                                                                                     0.0s
 => => transferring context: 34B                                                                                      0.0s
 => [internal] load metadata for docker.io/library/node:16-alpine                                                     0.5s
 => [1/6] FROM docker.io/library/node:16-alpine@sha256:b4a72f83f52bbe3970bb74a15e44ec4cf6e873ad4787473dfc8a26f5b4e29  0.0s
 => [internal] load build context                                                                                     0.1s
 => => transferring context: 24.22kB                                                                                  0.1s
 => CACHED [2/6] WORKDIR /app                                                                                         0.0s
 => CACHED [3/6] COPY package*.json ./                                                                                0.0s
 => CACHED [4/6] RUN npm install --quiet                                                                              0.0s
 => [5/6] COPY . .                                                                                                    0.0s
 => [6/6] RUN npm run build                                                                                           8.9s
 => exporting to image                                                                                                0.0s
 => => exporting layers                                                                                               0.0s
 => => writing image sha256:478016d17cf8826bbbb94faecc2a4f556a5f77280c41342bd27b1b200f2870ba                          0.0s 
 => => naming to docker.io/library/ace-image                                                                          0.0s 

运行日志

➜  ace git:(main) ✗ docker run -p 3000:3000 ace-image

> ace@0.1.0 start
> react-scripts start --quiet

(node:26) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
(Use `node --trace-deprecation ...` to show where the warning was created)
(node:26) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
Starting the development server...

Compiled successfully!

You can now view ace in the browser.

  Local:            http://localhost:3000
  On Your Network:  http://172.17.0.2:3000

Note that the development build is not optimized.
To create a production build, use npm run build.

webpack compiled successfully
Compiling...
Compiled successfully!
webpack compiled successfully

Dockerfile内容

# Set the base image to Node 16
FROM node:16-alpine

# Set the working directory in the container to /app
WORKDIR /app

# Copy the package.json and package-lock.json files to the container
COPY package*.json ./

# Install dependencies quietly
RUN npm install --quiet

# Copy the rest of the application code to the container
COPY . .

# Build the application
RUN npm run build

# Expose port 3000
EXPOSE 3000

# Run the command to start the server
CMD ["npm", "start", "--", "--quiet"]
解决方案
  • 升级react-scripts版本:
    警告根源是旧版本react-scripts使用了webpack-dev-server已废弃的API。将react-scripts升级到v5.x及以上稳定版本,官方已替换onBeforeSetupMiddleware和onAfterSetupMiddleware为setupMiddlewares,升级后警告会自动消除。执行命令:

    npm install react-scripts@latest --save
    

    之后重新构建Docker镜像即可。

  • 临时禁用特定警告:
    若暂时无法升级,可通过Node.js环境变量屏蔽这两个特定废弃警告。修改Dockerfile中的配置:

    ENV NODE_OPTIONS="--no-warnings=DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE,DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE"
    CMD ["npm", "start"]
    

    或直接指定node命令运行启动脚本:

    CMD ["node", "--no-warnings=DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE,DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE", "./node_modules/react-scripts/scripts/start.js"]
    

    此方法仅隐藏警告,不解决根本问题,建议优先选择升级方案。

  • 检查自定义webpack配置:
    若使用eject、craco或react-app-rewired等工具自定义了webpack配置,需手动将配置中的onBeforeSetupMiddleware和onAfterSetupMiddleware替换为setupMiddlewares。示例替换方式:
    旧配置:

    devServer: {
      onBeforeSetupMiddleware: (devServer) => {
        // 自定义逻辑
      },
      onAfterSetupMiddleware: (devServer) => {
        // 自定义逻辑
      }
    }
    

    新配置:

    devServer: {
      setupMiddlewares: (middlewares, devServer) => {
        // 原onBeforeSetupMiddleware逻辑
        // 原onAfterSetupMiddleware逻辑(按顺序排列)
        return middlewares;
      }
    }
    

内容的提问来源于stack exchange,提问作者code-8

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 05:02:10