使用 Webpack 5 构建 ES 模块包,并在 Node.js ES 模块中使用该包

Use Webpack 5 to build an ES module bundle, and consume that bundle in a Node.js ES module

是否可以使用 Webpack 5 构建 ES 模块包(默认导出),然后在 Node.js ES 模块中使用(导入)该包?

我有以下文件:

|- webpack.config.js
|- package.json
|- /src
  |- index.js

index.js

function util() {
    return "I'm a util!";
}
export default util;

webpack.config.js

const path = require('path');

module.exports = {
    entry: './src/index.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    experiments: {
        outputModule: true,
    },
};

package.json

{
  "name": "exporter",
  "version": "1.0.0",
  "module": "dist/index.js",
  "scripts": {
    "build": "webpack"
  }
}

经过运行宁npm run build,我们得到:

|- webpack.config.js
|- package.json
|- /src
  |- index.js
|- /dist
  |- bundle.js

太好了,现在我只想创建一些“演示”文件 importer.js,它将导入 util 并使用它。为方便起见,我将在同一文件夹中创建它:

|- importer.js
|- webpack.config.js
|- package.json
|- /src
  |- index.js
|- /dist
  |- bundle.js

importer.js

import util from './dist/bundle.js';

console.log(util());

现在我 运行 node importer.js,我得到这个(预期的)错误:

Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
[...]
SyntaxError: Cannot use import statement outside a module

好吧,让我们把 "type": "module" 添加到 package.json:

package.json

{
  "name": "exporter",
  "version": "1.0.0",
  "module": "dist/index.js",
  "scripts": {
    "build": "webpack"
  },
  "type": "module"
}

现在,再试一次 node importer.js 给我们另一个错误:

SyntaxError: The requested module './dist/bundle.js' does not provide an export named 'default'

更重要的是,在尝试重新运行 npm run build 时,我们得到了另一个错误:

[webpack-cli] Failed to load 'path\to\webpack.config.js' config
[webpack-cli] ReferenceError: require is not defined

我怎样才能让它发挥作用?

由于您使用 type: "module",Node.js 会将此模块视为 ESM。根据文档 esm_no_require_exports_or_module_exportsrequiremodule.exports__dirname 在 ESM 中不可用。我将使用 ESM 语法重写 webpack.config.js.

__dirname 变量创建一个 polyfill。

来自 webpack 文档:

If you're using webpack to compile a library to be consumed by others, make sure to set output.libraryTarget to 'module' when output.module is true.

例如

webpack.config.js:

import path from "path";
import { fileURLToPath } from "url";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

export default {
  mode: "development",     
  entry: "./src/index.js",
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "dist"),
    library: {
      type: "module",
    },
  },
  experiments: {
    outputModule: true,
  },
};

package.json:

{
  "name": "exporter",
  "version": "1.0.0",
  "main": "dist/bundle.js",
  "scripts": {
    "build": "webpack"
  },
  "devDependencies": {
    "webpack": "^5.51.1",
    "webpack-cli": "^4.8.0"
  },
  "type": "module"
}

./src/index.js:

function util() {
  return "I'm a util!";
}
export default util;

importer.js:

import util from "./dist/bundle.js";

console.log(util());

构建:

⚡  npm run build   

> exporter@1.0.0 build /Users/dulin/workspace/github.com/mrdulin/webpack-samples/packages/webpack-v5/Whosebug/68913996
> webpack

asset bundle.js 3.01 KiB [compared for emit] [javascript module] (name: main)
runtime modules 670 bytes 3 modules
./src/index.js 65 bytes [built] [code generated]
webpack 5.51.1 compiled successfully in 87 ms

执行importer.js:

⚡  node importer.js 
I'm a util!