本指南旨在解释什么是 ES 模块,以及如何使 Nuxt 应用(或上游库)与 ESM 兼容。
CommonJS (CJS) 是 Node.js 引入的一种格式,它允许在隔离的 JavaScript 模块之间共享功能(阅读更多)。您可能已经熟悉这种语法
const a = require('./a')
module.exports.a = a
像 webpack 和 Rollup 这样的打包工具支持这种语法,并允许您在浏览器中使用用 CommonJS 编写的模块。
大多数情况下,当人们谈论 ESM 与 CJS 的区别时,他们指的是编写代码时的一种不同语法modules.
import a from './a'
export { a }
在 ECMAScript 模块 (ESM) 成为标准之前(这花了超过 10 年!),像webpack甚至像 TypeScript 这样的语言也开始支持所谓的 **ESM 语法**。然而,与实际规范存在一些关键区别;这里有一个有用的解释.
您可能已经使用 ESM 语法编写应用很长时间了。毕竟,浏览器原生支持它,在 Nuxt 2 中,我们会将您编写的所有代码编译成适当的格式(服务器使用 CJS,浏览器使用 ESM)。
当向包中添加模块时,情况略有不同。一个示例库可能会同时暴露 CJS 和 ESM 版本,并让我们选择我们想要的
{
"name": "sample-library",
"main": "dist/sample-library.cjs.js",
"module": "dist/sample-library.esm.js"
}
因此,在 Nuxt 2 中,打包工具(webpack)会在服务器构建中拉取 CJS 文件('main'),并在客户端构建中使用 ESM 文件('module')。
然而,在最新的 Node.js LTS 版本中,现在可以在使用原生 ESM 模块在 Node.js 中。这意味着 Node.js 本身可以处理使用 ESM 语法的 JavaScript,尽管默认情况下它不这样做。启用 ESM 语法的最常见方法有两种
package.json 中设置 "type": "module" 并继续使用 .js 扩展名.mjs 文件扩展名(推荐)这就是我们为 Nuxt Nitro 所做的;我们输出一个 .output/server/index.mjs 文件。这告诉 Node.js 将该文件视为原生 ES 模块。
当您 import 一个模块而不是 require 它时,Node.js 的解析方式不同。例如,当您导入 sample-library 时,Node.js 会查找该库 package.json 中的 exports 条目,如果未定义 exports,则回退到 main 条目。
这对于动态导入也是如此,例如 const b = await import('sample-library')。
Node 支持以下类型的导入(参见文档):
.mjs 结尾的文件 - 预期使用 ESM 语法.cjs 结尾的文件 - 预期使用 CJS 语法.js 结尾的文件 - 预期使用 CJS 语法,除非其 package.json 具有 "type": "module"长期以来,模块作者一直在生成 ESM 语法的构建,但使用 .esm.js 或 .es.js 等约定,并将其添加到 package.json 中的 module 字段。直到现在这都不是问题,因为它们只被像 webpack 这样的打包工具使用,而这些工具不太关心文件扩展名。
但是,如果您尝试在 Node.js ESM 上下文中导入一个带有 .esm.js 文件的包,它将不起作用,并且您会收到类似以下错误
(node:22145) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
/path/to/index.js:1
export default {}
^^^^^^
SyntaxError: Unexpected token 'export'
at wrapSafe (internal/modules/cjs/loader.js:1001:16)
at Module._compile (internal/modules/cjs/loader.js:1049:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
....
at async Object.loadESM (internal/process/esm_loader.js:68:5)
如果您从 Node.js 认为是 CJS 的 ESM 语法构建进行命名导入,也可能收到此错误
file:///path/to/index.mjs:5
import { named } from 'sample-library'
^^^^^
SyntaxError: Named export 'named' not found. The requested module 'sample-library' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'sample-library';
const { named } = pkg;
at ModuleJob._instantiate (internal/modules/esm/module_job.js:120:21)
at async ModuleJob.run (internal/modules/esm/module_job.js:165:5)
at async Loader.import (internal/modules/esm/loader.js:177:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5)
如果遇到这些错误,问题几乎肯定出在上游库。他们需要修复他们的库以支持被 Node.js 导入。
在此期间,您可以通过将它们添加到 build.transpile 来告诉 Nuxt 不要尝试导入这些库
export default defineNuxtConfig({
build: {
transpile: ['sample-library'],
},
})
您可能会发现您也需要添加这些库正在导入的其他包。
在某些情况下,您可能还需要手动将库别名为 CJS 版本,例如
export default defineNuxtConfig({
alias: {
'sample-library': 'sample-library/dist/sample-library.cjs.js',
},
})
使用 CommonJS 格式的依赖项,可以使用 module.exports 或 exports 来提供默认导出
module.exports = { test: 123 }
// or
exports.test = 123
当我们 require 这样的依赖项时,这通常工作得很好
const pkg = require('cjs-pkg')
console.log(pkg) // { test: 123 }
以原生 ESM 模式运行的 Node.js, 启用了 esModuleInterop 的 TypeScript以及像 webpack 这样的打包工具,提供了一个兼容机制,使我们能够默认导入这样的库。这个机制通常被称为“互操作性 require default”
import pkg from 'cjs-pkg'
console.log(pkg) // { test: 123 }
然而,由于语法检测的复杂性和不同的打包格式,总是有可能互操作默认值失败,最终得到如下结果
import pkg from 'cjs-pkg'
console.log(pkg) // { default: { test: 123 } }
另外,在使用动态导入语法时(在 CJS 和 ESM 文件中),我们总是遇到这种情况
import('cjs-pkg').then(console.log) // [Module: null prototype] { default: { test: '123' } }
在这种情况下,我们需要手动处理默认导出
// Static import
import { default as pkg } from 'cjs-pkg'
// Dynamic import
import('cjs-pkg').then(m => m.default || m).then(console.log)
为了处理更复杂的情况和更高的安全性,我们推荐并在内部使用mlly在 Nuxt 中,它可以保留命名导出。
import { interopDefault } from 'mlly'
// Assuming the shape is { default: { foo: 'bar' }, baz: 'qux' }
import myModule from 'my-module'
console.log(interopDefault(myModule)) // { foo: 'bar', baz: 'qux' }
好消息是修复 ESM 兼容性问题相对简单。主要有两种选择
.mjs 结尾。.cjs 结尾,以获得最大的明确性。package.json 中设置 "type": "module" 并确保您的构建库使用 ESM 语法。然而,您的依赖项可能会遇到问题——并且这种方法意味着您的库只能在 ESM 上下文中被使用。从 CJS 到 ESM 的初始步骤是将任何对 require 的使用替换为使用 import
module.exports = function () { /* ... */ }
exports.hello = 'world'
export default function () { /* ... */ }
export const hello = 'world'
const myLib = require('my-lib')
import myLib from 'my-lib'
// or
const dynamicMyLib = await import('my-lib').then(lib => lib.default || lib)
在 ESM 模块中,与 CJS 不同,require、require.resolve、__filename 和 __dirname 全局变量不可用,应替换为 import() 和 import.meta.filename。
const { join } = require('node:path')
const newDir = join(__dirname, 'new-dir')
import { fileURLToPath } from 'node:url'
const newDir = fileURLToPath(new URL('./new-dir', import.meta.url))
const someFile = require.resolve('./lib/foo.js')
import { resolvePath } from 'mlly'
const someFile = await resolvePath('my-lib', { url: import.meta.url })
exports 字段和条件导出。(阅读更多).{
"exports": {
".": {
"import": "./dist/mymodule.mjs"
}
}
}