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

使用BundleDeclarationsWebpackPlugin合并TS文件时遇TypeError:不是构造函数,求排查配置

问题分析与解决

出现TypeError: BundleDeclarationsWebpackPlugin is not a constructor的核心原因是插件引入方式错误:

bundle-declarations-webpack-plugin 以ES模块规范编写,用CommonJS的require()引入时,默认导出会被包裹在.default属性里,你当前的代码直接引入了整个模块对象,而非插件的构造函数本身。

修复步骤

修改插件引入代码,添加.default:

const BundleDeclarationsWebpackPlugin = require("bundle-declarations-webpack-plugin").default;

修改后的完整配置片段:

const path = require('path');
const nodeExternals = require('webpack-node-externals');
const BundleDeclarationsWebpackPlugin = require("bundle-declarations-webpack-plugin").default; // 此处添加.default

module.exports = {
  entry: './src/index.ts',
  mode: 'production',
  output: {
   ...
  },
  devtool: 'source-map',
  module: {
      ...
  },
  plugins: [
    new BundleDeclarationsWebpackPlugin({
      entry: './dist/types/index.d.ts',
      outFile: './dist/index.d.ts',
    }),
  ],
  resolve: {
    extensions: ['.tsx', '.ts', '.js']
  }
};

额外检查点

  • 确保bundle-declarations-webpack-plugin版本与你的webpack版本兼容(比如webpack 5需搭配插件最新版本)
  • 确认entry配置的.d.ts文件路径正确,webpack执行时该文件已通过tsc编译生成

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 10:32:36