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

NestJS v9.4.2集成pg v8.6.0失败,Webpack报cloudflare:sockets错误

解决Nest.js v9.4.2 + pg v8.6.0的Webpack cloudflare:sockets构建错误

问题描述

使用Nest.js v9.4.2集成pg v8.6.0时,Webpack构建触发以下错误:

Info  Webpack is building your sources...

ERROR in cloudflare:sockets
Module build failed: UnhandledSchemeError: Reading from "cloudflare:sockets" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "cloudflare:" URIs.

尝试用IgnorePlugin忽略该模块,但未生效。

解决方案

pg v8.6.0新增了Cloudflare Workers环境的支持,引入了cloudflare:sockets模块,但Node.js环境下不需要该模块。以下三种方案可解决问题:

方案1:替换为Node.js原生net模块

将cloudflare:sockets替换为pg在Node环境下原本依赖的net模块,修改Webpack配置文件:

const webpack = require('webpack');

module.exports = {
  webpack: (config, options) => {
    config.plugins.push(
      new webpack.NormalModuleReplacementPlugin(
        /^cloudflare:sockets$/,
        (resource) => {
          resource.request = 'net';
        }
      )
    );

    return config;
  },
};

方案2:直接忽略该模块

确认不需要Cloudflare相关功能时,可直接忽略cloudflare:sockets模块:

const webpack = require('webpack');

module.exports = {
  webpack: (config, options) => {
    config.plugins.push(
      new webpack.IgnorePlugin({
        checkResource: (resource) => {
          return resource === 'cloudflare:sockets';
        },
      }),
    );

    return config;
  },
};

方案3:配置resolve.fallback

通过Webpack的resolve.fallback标记该模块无需加载:

module.exports = {
  webpack: (config, options) => {
    config.resolve.fallback = {
      ...config.resolve.fallback,
      'cloudflare:sockets': false,
    };

    return config;
  },
};

原配置无效原因

原IgnorePlugin的resourceRegExp未正确匹配cloudflare:sockets这类协议式模块导入路径。Webpack对协议式URI的解析逻辑与普通模块不同,使用NormalModuleReplacementPlugin或resolve.fallback能更精准处理这类特殊模块。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 15:38:34