搜索
首页web前端js教程Node.js 和 esbuild:小心混合使用 cjs 和 esm

Node.js and esbuild: beware of mixing cjs and esm

长话短说

当使用 esbuild 将代码与 --platform=node 捆绑在一起(依赖于混合了 cjs 和 esm 入口点的 npm 包)时,请使用以下经验法则:

  • 使用--bundle时,将--format设置为cjs。这适用于除具有顶级等待的 esm 模块之外的所有情况。
    • --format=esm 可以使用,但需要一个像这样的polyfill。
  • 使用--packages=external时,将--format设置为esm。

如果您想知道 cjs 和 esm 之间的区别,请查看 Node.js:cjs、捆绑程序和 esm 的简史。

症状

使用 --platform=node 执行 esbuild 捆绑代码时,您可能会遇到以下运行时错误之一:

Error: Dynamic require of "<module_name>" is not supported
</module_name>
Error [ERR_REQUIRE_ESM]: require() of ES Module (...) from (...) not supported.
Instead change the require of (...) in (...) to a dynamic import() which is available in all CommonJS modules.

原因

这是因为以下限制之一:

  • esbuild 的 esm 到 cjs(反之亦然)转换。
  • Node.js cjs/esm 互操作性。

分析

esbuild 在 esm 和 cjs 之间的转换能力有限。此外,某些场景虽然受 esbuild 支持,但 Node.js 本身并不支持。从 esbuild@0.24.0 开始,下表总结了支持的内容:

Format Scenario Supported?
cjs static import Yes
cjs dynamic import() Yes
cjs top-level await No
cjs --packages=external of esm entry point No*
esm require() of user modules** Yes***
esm require() of node:* modules No****
esm --packages=external of cjs entry point Yes

* esbuild 支持,但 Node.js 不支持

** 指 npm 包或相对路径文件。

*** 支持用户模块,但有一些注意事项:如果没有腻子填充,则不支持 __dirname 和 __filename。

**** 节点:* 模块可以使用相同的 polyfill 支持。

以下是这些场景的详细描述,不使用任何填充:


npm 包

我们将使用以下示例 npm 包:

静态导入

具有静态导入的esm模块:

Error: Dynamic require of "<module_name>" is not supported
</module_name>

动态导入

esm 模块在异步函数中具有动态 import():

Error [ERR_REQUIRE_ESM]: require() of ES Module (...) from (...) not supported.
Instead change the require of (...) in (...) to a dynamic import() which is available in all CommonJS modules.

顶级等待

具有动态 import() 和顶级等待的 esm 模块:

import { version } from "node:process";

export function getVersion() {
  return version;
}

要求

带有 require() 调用的 cjs 模块:

export async function getVersion() {
  const { version } = await import("node:process");
  return version;
}

--格式=cjs

我们将使用以下参数运行 esbuild:

const { version } = await import("node:process");

export function getVersion() {
  return version;
}

和以下代码:

const { version } = require("node:process");

exports.getVersion = function() {
  return version;
}

静态导入

产生以下运行良好的内容:

esbuild --bundle --format=cjs --platform=node --outfile=bundle.cjs src/main.js

动态导入()

产生以下运行良好的内容:

import { getVersion } from "{npm-package}";

(async () => {
  // version can be `string` or `Promise<string>`
  const version = await getVersion();

  console.log(version);
})();
</string>

注意动态 import() 没有转换为 require(),因为它在 cjs 模块中也是允许的。

顶级等待

esbuild 失败并出现以下错误:

// node_modules/static-import/index.js
var import_node_process = require("node:process");
function getVersion() {
  return import_node_process.version;
}

// src/main.js
(async () => {
  const version2 = await getVersion();
  console.log(version2);
})();

--packages=外部

对所有 npm 包使用 --packages=external 都会成功:

// (...esbuild auto-generated helpers...)

// node_modules/dynamic-import/index.js
async function getVersion() {
  const { version } = await import("node:process");
  return version;
}

// src/main.js
(async () => {
  const version = await getVersion();
  console.log(version);
})();

产生:

[ERROR] Top-level await is currently not supported with the "cjs" output format

    node_modules/top-level-await/index.js:1:20:
      1 │ const { version } = await import("node:process");
        ╵                     ~~~~~

但是,它们都无法运行,因为 Nodes.js 不允许 cjs 模块导入 esm 模块:

esbuild --packages=external --format=cjs --platform=node --outfile=bundle.cjs src/main.js

--格式=esm

我们现在将使用以下参数运行 esbuild:

var npm_package_import = require("{npm-package}");
(async () => {
  const version = await (0, npm_package_import.getVersion)();
  console.log(version);
})();

用户模块的 require()

src/main.js

/(...)/bundle.cjs:1
var import_static_import = require("static-import");
                           ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /(...)/node_modules/static-import/index.js from /(...)/bundle.cjs not supported.
Instead change the require of index.js in /(...)/bundle.cjs to a dynamic import() which is available in all CommonJS modules.

产生以下运行良好的结果:

esbuild --bundle --format=esm --platform=node --outfile=bundle.mjs src/main.js

节点的 require():* 模块

src/main.js

const { getVersion } = require("static-import");

console.log(getVersion());

产生以下内容:

// (...esbuild auto-generated helpers...)

// node_modules/static-import/index.js
var static_import_exports = {};
__export(static_import_exports, {
  getVersion: () => getVersion
});
import { version } from "node:process";
function getVersion() {
  return version;
}
var init_static_import = __esm({
  "node_modules/static-import/index.js"() {
  }
});

// src/main.js
var { getVersion: getVersion2 } = (init_static_import(), __toCommonJS(static_import_exports));
console.log(getVersion2());

但是,它无法运行:

import { getVersion } from "require";

console.log(getVersion());

--packages=外部

对所有 npm 包使用 --packages=external 都会成功,包括那些带有 cjs 入口点的包。例如:

// (...esbuild auto-generated helpers...)

var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
  get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
  if (typeof require !== "undefined") return require.apply(this, arguments);
  throw Error('Dynamic require of "' + x + '" is not supported');
});

// (...esbuild auto-generated helpers...)

// node_modules/require/index.js
var require_require = __commonJS({
  "node_modules/require/index.js"(exports) {
    var { version } = __require("node:process");
    exports.getVersion = function() {
      return version;
    };
  }
});

// src/main.js
var import_require = __toESM(require_require());
console.log((0, import_require.getVersion)());

与:

src/index.js

Error: Dynamic require of "node:process" is not supported

产生几乎逐字输出,运行得很好,因为 esm 模块可以使用 cjs 入口点导入 npm 包:

esbuild --packages=external --format=esm --platform=node --outfile=bundle.mjs src/main.js

结论

我希望您发现这篇文章对于现在和将来解决 esbuild 输出问题很有用。请在下面告诉我你的想法!

以上是Node.js 和 esbuild:小心混合使用 cjs 和 esm的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python vs. JavaScript:选择合适的工具Python vs. JavaScript:选择合适的工具May 08, 2025 am 12:10 AM

选择Python还是JavaScript取决于项目类型:1)数据科学和自动化任务选择Python;2)前端和全栈开发选择JavaScript。Python因其在数据处理和自动化方面的强大库而备受青睐,而JavaScript则因其在网页交互和全栈开发中的优势而不可或缺。

Python和JavaScript:了解每个的优势Python和JavaScript:了解每个的优势May 06, 2025 am 12:15 AM

Python和JavaScript各有优势,选择取决于项目需求和个人偏好。1.Python易学,语法简洁,适用于数据科学和后端开发,但执行速度较慢。2.JavaScript在前端开发中无处不在,异步编程能力强,Node.js使其适用于全栈开发,但语法可能复杂且易出错。

JavaScript的核心:它是在C还是C上构建的?JavaScript的核心:它是在C还是C上构建的?May 05, 2025 am 12:07 AM

javascriptisnotbuiltoncorc; saninterpretedlanguagethatrunsonenginesoftenwritteninc.1)javascriptwasdesignedAsalightweight,解释edganguageforwebbrowsers.2)Enginesevolvedfromsimpleterterterpretpreterterterpretertestojitcompilerers,典型地提示。

JavaScript应用程序:从前端到后端JavaScript应用程序:从前端到后端May 04, 2025 am 12:12 AM

JavaScript可用于前端和后端开发。前端通过DOM操作增强用户体验,后端通过Node.js处理服务器任务。1.前端示例:改变网页文本内容。2.后端示例:创建Node.js服务器。

Python vs. JavaScript:您应该学到哪种语言?Python vs. JavaScript:您应该学到哪种语言?May 03, 2025 am 12:10 AM

选择Python还是JavaScript应基于职业发展、学习曲线和生态系统:1)职业发展:Python适合数据科学和后端开发,JavaScript适合前端和全栈开发。2)学习曲线:Python语法简洁,适合初学者;JavaScript语法灵活。3)生态系统:Python有丰富的科学计算库,JavaScript有强大的前端框架。

JavaScript框架:为现代网络开发提供动力JavaScript框架:为现代网络开发提供动力May 02, 2025 am 12:04 AM

JavaScript框架的强大之处在于简化开发、提升用户体验和应用性能。选择框架时应考虑:1.项目规模和复杂度,2.团队经验,3.生态系统和社区支持。

JavaScript,C和浏览器之间的关系JavaScript,C和浏览器之间的关系May 01, 2025 am 12:06 AM

引言我知道你可能会觉得奇怪,JavaScript、C 和浏览器之间到底有什么关系?它们之间看似毫无关联,但实际上,它们在现代网络开发中扮演着非常重要的角色。今天我们就来深入探讨一下这三者之间的紧密联系。通过这篇文章,你将了解到JavaScript如何在浏览器中运行,C 在浏览器引擎中的作用,以及它们如何共同推动网页的渲染和交互。JavaScript与浏览器的关系我们都知道,JavaScript是前端开发的核心语言,它直接在浏览器中运行,让网页变得生动有趣。你是否曾经想过,为什么JavaScr

node.js流带打字稿node.js流带打字稿Apr 30, 2025 am 08:22 AM

Node.js擅长于高效I/O,这在很大程度上要归功于流。 流媒体汇总处理数据,避免内存过载 - 大型文件,网络任务和实时应用程序的理想。将流与打字稿的类型安全结合起来创建POWE

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境