ESM CJS 互操

Sun May 11 2025 · 10min

在CJS里引用ESM

只能用import()

const { default: pkg } = await import('esm-only-package')

在ESM里引用CJS

推荐使用default import

// cjs.cjs
exports.name = 'exported';
import { name } from './cjs.cjs';
console.log(name);
// Prints: 'exported'

import cjs from './cjs.cjs';
console.log(cjs);
// Prints: { name: 'exported' }

import * as m from './cjs.cjs';
console.log(m);
// Prints: [Module] { default: { name: 'exported' }, name: 'exported' }

上下文shim

require require.resolve

import { createRequire } from 'node:module'

const require = createRequire(import.meta.url)


__dirname __filename

import { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

const _dirname = typeof __dirname !== 'undefined'
  ? __dirname
  : dirname(fileURLToPath(import.meta.url))

参考

https://nodejs.org/api/esm.html#interoperability-with-commonjs