ESModule

ESModule only

目前有些库采取了esmodule only的处理方式,可以从package.json中看到:

Node.js 默认将 JavaScript 代码视为 CommonJS 模块。 作者可以通过 .mjs 文件扩展名、package.json “type” 字段、或 —input-type 标志告诉 Node.js 将 JavaScript 代码视为 ECMAScript 模块。 有关更多详细信息,请参阅包模块

node >= 23,type 设置为 module,以及拥有 exports 作为导出

  1. {
  2. "name": "escape-goat",
  3. "version": "4.0.0",
  4. "description": "Escape a string for use in HTML or the inverse",
  5. "license": "MIT",
  6. "repository": "sindresorhus/escape-goat",
  7. "author": {
  8. "name": "Sindre Sorhus",
  9. "email": "sindresorhus@gmail.com",
  10. "url": "https://sindresorhus.com"
  11. },
  12. "type": "module",
  13. "exports": "./index.js",
  14. "engines": {
  15. "node": ">=12"
  16. },
  17. "scripts": {
  18. "test": "xo && ava && tsd"
  19. },
  20. "files": [
  21. "index.js",
  22. "index.d.ts"
  23. ],
  24. }

exports 说明:https://medium.com/swlh/npm-new-package-json-exports-field-1a7d1f489ccf
exports 官方文档:https://nodejs.org/api/packages.html#packages_exports