在Webpack中使用ES Module来加载CommonJS模块,可以使用@std/esm
这个库。具体步骤如下:
-
安装
@std/esm
库:npm install @std/esm --save-dev
-
在Webpack配置文件中添加以下代码:
require = require("@std/esm")(module/*, options*/) module.exports = require("./your-entry-file.js")
这段代码的作用是将require函数替换为@std/esm库提供的函数,从而使得Webpack能够正确地加载CommonJS模块。
-
在你的代码中,使用ES Module的语法来加载CommonJS模块:
import * as commonjsModule from './path/to/commonjs-module.js'
这样就可以使用ES Module的语法来加载CommonJS模块了。