{"_id":"load-esm","_rev":"4082154","name":"load-esm","description":"Utility to dynamically load ESM modules in TypeScript CommonJS projects","dist-tags":{"latest":"1.0.3"},"maintainers":[{"name":"borewit","email":"borewit@xs4all.nl"}],"time":{"modified":"2025-11-24T21:39:00.000Z","created":"2024-12-09T10:28:35.469Z","1.0.3":"2025-08-24T12:09:33.627Z","1.0.2":"2025-02-28T10:03:22.298Z","1.0.1":"2024-12-18T21:06:36.359Z","1.0.0":"2024-12-13T00:52:17.050Z","0.1.1":"2024-12-11T09:31:14.798Z","0.1.0":"2024-12-09T10:28:35.469Z"},"users":{},"author":{"name":"Borewit","url":"https://github.com/Borewit"},"repository":{"type":"git","url":"git+https://github.com/Borewit/load-esm.git"},"versions":{"1.0.3":{"name":"load-esm","version":"1.0.3","type":"commonjs","main":"index.js","types":"index.d.ts","scripts":{"test":"mocha","compile-test":"tsc -p test"},"author":{"name":"Borewit","url":"https://github.com/Borewit"},"funding":[{"type":"github","url":"https://github.com/sponsors/Borewit"},{"type":"buymeacoffee","url":"https://buymeacoffee.com/borewit"}],"license":"MIT","description":"Utility to dynamically load ESM modules in TypeScript CommonJS projects","repository":{"type":"git","url":"git+https://github.com/Borewit/load-esm.git"},"bugs":{"url":"https://github.com/Borewit/load-esm/issues"},"homepage":"https://github.com/Borewit/load-esm#readme","keywords":["load-esm","ESM","Import ESM","CJS","CommonJS","TypeScript","Node.js","dynamic import","dynamic module loading","dynamic load","JavaScript modules","CJS ESM interoperability","import ESM in CommonJS","typescript commonjs import esm","module interoperability","node esm loader","mixed module","dynamic esm import typescript","cjs to esm bridge","ERR_REQUIRE_ESM","require ESM error","ERR_PACKAGE_PATH_NOT_EXPORTED","No \"exports\" main defined in","exports not defined error"],"engines":{"node":">=13.2.0"},"devDependencies":{"@types/chai":"^5.0.1","@types/mocha":"^10.0.10","@types/node":"^22.10.2","chai":"^4.3.4","mocha":"^11.0.1","typescript":"^5.7.2"},"_id":"load-esm@1.0.3","gitHead":"d5623ec42643c93ec21c9f2ff295dc3d2915f6c9","_nodeVersion":"22.17.0","_npmVersion":"11.4.2","dist":{"shasum":"2073afe3da63902c323e80d9f135c301173ac92c","size":3190,"noattachment":false,"key":"/load-esm/-/load-esm-1.0.3.tgz","tarball":"http://registry.cnpm.dingdandao.com/load-esm/download/load-esm-1.0.3.tgz"},"_npmUser":{"name":"borewit","email":"borewit@xs4all.nl"},"directories":{},"maintainers":[{"name":"borewit","email":"borewit@xs4all.nl"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/load-esm_1.0.3_1756037373447_0.21271931628791907"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2025-08-24T12:09:33.627Z","publish_time":1756037373627,"_source_registry_name":"default","_cnpm_publish_time":1756037373627},"1.0.2":{"name":"load-esm","version":"1.0.2","main":"index.js","types":"index.d.ts","scripts":{"test":"mocha","compile-test":"tsc -p test"},"author":{"name":"Borewit","url":"https://github.com/Borewit"},"funding":[{"type":"github","url":"https://github.com/sponsors/Borewit"},{"type":"buymeacoffee","url":"https://buymeacoffee.com/borewit"}],"license":"MIT","description":"Utility to dynamically load ESM modules in TypeScript CommonJS projects","repository":{"type":"git","url":"git+https://github.com/Borewit/load-esm.git"},"bugs":{"url":"https://github.com/Borewit/load-esm/issues"},"homepage":"https://github.com/Borewit/load-esm#readme","keywords":["load-esm","ESM","Import ESM","CommonJS","TypeScript","Node.js","dynamic import","dynamic load","JavaScript modules","module interoperability","mixed-module environment","ERR_REQUIRE_ESM","require() of ES Module","ERR_PACKAGE_PATH_NOT_EXPORTED","No \"exports\" main defined in"],"engines":{"node":">=13.2.0"},"devDependencies":{"@types/chai":"^5.0.1","@types/mocha":"^10.0.10","@types/node":"^22.10.2","chai":"^4.3.4","mocha":"^11.0.1","typescript":"^5.7.2"},"_id":"load-esm@1.0.2","gitHead":"27696a447e06f827981df3383292be047d6b4faf","_nodeVersion":"22.13.0","_npmVersion":"10.9.2","dist":{"shasum":"35dbac8a1a3abdb802cf236008048fcc8a9289a6","size":2844,"noattachment":false,"key":"/load-esm/-/load-esm-1.0.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/load-esm/download/load-esm-1.0.2.tgz"},"_npmUser":{"name":"borewit","email":"borewit@xs4all.nl"},"directories":{},"maintainers":[{"name":"borewit","email":"borewit@xs4all.nl"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/load-esm_1.0.2_1740737002094_0.26789791825024767"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2025-02-28T10:03:22.298Z","publish_time":1740737002298,"_source_registry_name":"default","_cnpm_publish_time":1740737002298},"1.0.1":{"name":"load-esm","version":"1.0.1","main":"index.js","types":"index.d.ts","scripts":{"test":"mocha","compile-test":"tsc -p test"},"author":{"name":"Borewit","url":"https://github.com/Borewit"},"funding":[{"type":"github","url":"https://github.com/sponsors/Borewit"},{"type":"buymeacoffee","url":"https://buymeacoffee.com/borewit"}],"license":"MIT","description":"Utility to dynamically load ESM modules in TypeScript CommonJS projects","repository":{"type":"git","url":"git+https://github.com/Borewit/load-esm.git"},"bugs":{"url":"https://github.com/Borewit/load-esm/issues"},"homepage":"https://github.com/Borewit/load-esm#readme","keywords":["load-esm","ESM","Import ESM","CommonJS","TypeScript","Node.js","dynamic import","dynamic load","JavaScript modules","module interoperability","mixed-module environment","ERR_PACKAGE_PATH_NOT_EXPORTED","No \"exports\" main defined in"],"engines":{"node":">=13.2.0"},"devDependencies":{"@types/chai":"^5.0.1","@types/mocha":"^10.0.10","@types/node":"^22.10.2","chai":"^4.3.4","mocha":"^11.0.1","typescript":"^5.7.2"},"_id":"load-esm@1.0.1","gitHead":"4251f22aa4a3ee7a640d731a4a6c302ffadd970e","_nodeVersion":"20.17.0","_npmVersion":"10.8.2","dist":{"shasum":"6ccd70a736e4a4d6296d488c365bd6afdf93b756","size":2658,"noattachment":false,"key":"/load-esm/-/load-esm-1.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/load-esm/download/load-esm-1.0.1.tgz"},"_npmUser":{"name":"borewit","email":"borewit@xs4all.nl"},"directories":{},"maintainers":[{"name":"borewit","email":"borewit@xs4all.nl"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/load-esm_1.0.1_1734555996119_0.8680346294276402"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2024-12-18T21:06:36.359Z","publish_time":1734555996359,"_source_registry_name":"default","_cnpm_publish_time":1734555996359},"1.0.0":{"name":"load-esm","version":"1.0.0","main":"index.cjs","types":"index.d.ts","author":{"name":"Borewit","url":"https://github.com/Borewit"},"funding":[{"type":"github","url":"https://github.com/sponsors/Borewit"},{"type":"buymeacoffee","url":"https://buymeacoffee.com/borewit"}],"license":"MIT","description":"Utility to dynamically load ESM modules in TypeScript CommonJS projects","repository":{"type":"git","url":"git+https://github.com/Borewit/load-esm.git"},"bugs":{"url":"https://github.com/Borewit/load-esm/issues"},"homepage":"https://github.com/Borewit/load-esm#readme","keywords":["load-esm","ESM","Import ESM","CommonJS","TypeScript","Node.js","dynamic import","dynamic load","JavaScript modules","module interoperability","mixed-module environment","ERR_PACKAGE_PATH_NOT_EXPORTED","No \"exports\" main defined in"],"engines":{"node":">=13.2.0"},"dependencies":{},"_id":"load-esm@1.0.0","gitHead":"3bc88296b4ce9a9a8c8cc5096998ded03da6f177","_nodeVersion":"20.17.0","_npmVersion":"10.8.2","dist":{"shasum":"ffeab57f68539a4e362d0015f52ef171b59174c2","size":2539,"noattachment":false,"key":"/load-esm/-/load-esm-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/load-esm/download/load-esm-1.0.0.tgz"},"_npmUser":{"name":"borewit","email":"borewit@xs4all.nl"},"directories":{},"maintainers":[{"name":"borewit","email":"borewit@xs4all.nl"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/load-esm_1.0.0_1734051136752_0.49276990677455634"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2024-12-13T00:52:17.050Z","publish_time":1734051137050,"_source_registry_name":"default","_cnpm_publish_time":1734051137050},"0.1.1":{"name":"load-esm","version":"0.1.1","main":"index.cjs","types":"index.d.ts","author":{"name":"Borewit","url":"https://github.com/Borewit"},"funding":[{"type":"github","url":"https://github.com/sponsors/Borewit"},{"type":"buymeacoffee","url":"https://buymeacoffee.com/borewit"}],"license":"MIT","description":"Utility to dynamically load ESM modules in TypeScript CommonJS projects","repository":{"type":"git","url":"git+https://github.com/Borewit/load-esm.git"},"bugs":{"url":"https://github.com/Borewit/load-esm/issues"},"homepage":"https://github.com/Borewit/load-esm#readme","keywords":["load-esm","ESM","Import ESM","CommonJS","TypeScript","Node.js","dynamic import","dynamic load"],"engines":{"node":">=13.2.0"},"dependencies":{},"_id":"load-esm@0.1.1","gitHead":"3c46d9ecb8dbbded6a7c7e1519064e4ba244c396","_nodeVersion":"22.11.0","_npmVersion":"10.9.0","dist":{"shasum":"af494802317907a1348379761370950e19e4877a","size":2376,"noattachment":false,"key":"/load-esm/-/load-esm-0.1.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/load-esm/download/load-esm-0.1.1.tgz"},"_npmUser":{"name":"borewit","email":"borewit@xs4all.nl"},"directories":{},"maintainers":[{"name":"borewit","email":"borewit@xs4all.nl"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/load-esm_0.1.1_1733909474634_0.03204671686588534"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2024-12-11T09:31:14.798Z","publish_time":1733909474798,"_source_registry_name":"default","_cnpm_publish_time":1733909474798},"0.1.0":{"name":"load-esm","version":"0.1.0","main":"index.cjs","types":"index.d.ts","author":{"name":"Borewit","url":"https://github.com/Borewit"},"funding":[{"type":"github","url":"https://github.com/sponsors/Borewit"},{"type":"buymeacoffee","url":"https://buymeacoffee.com/borewit"}],"license":"MIT","description":"Utility to dynamically load ESM modules in TypeScript CommonJS projects","repository":{"type":"git","url":"git+https://github.com/Borewit/load-esm.git"},"bugs":{"url":"https://github.com/Borewit/load-esm/issues"},"homepage":"https://github.com/Borewit/load-esm#readme","keywords":["load-esm","ESM","CommonJS","TypeScript","Node.js","dynamic import","JavaScript modules","module interoperability","mixed-module environment"],"engines":{"node":">=13.2.0"},"dependencies":{},"_id":"load-esm@0.1.0","gitHead":"029f62d5d9af00bbb4ad916e7af6b4e9d6c2c32e","_nodeVersion":"22.11.0","_npmVersion":"10.9.0","dist":{"shasum":"f4901c37d51fdb93618977fb83ae2a9acd5cfdbc","size":2237,"noattachment":false,"key":"/load-esm/-/load-esm-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/load-esm/download/load-esm-0.1.0.tgz"},"_npmUser":{"name":"borewit","email":"borewit@xs4all.nl"},"directories":{},"maintainers":[{"name":"borewit","email":"borewit@xs4all.nl"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/load-esm_0.1.0_1733740115304_0.32793554409017633"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2024-12-09T10:28:35.469Z","publish_time":1733740115469,"_source_registry_name":"default","_cnpm_publish_time":1733740115469}},"readme":"[![Node.js CI](https://github.com/Borewit/load-esm/actions/workflows/nodejs-ci.yml/badge.svg)](https://github.com/Borewit/load-esm/actions/workflows/nodejs-ci.yml)\n[![NPM version](https://img.shields.io/npm/v/load-esm.svg)](https://npmjs.org/package/load-esm)\n[![npm downloads](http://img.shields.io/npm/dm/load-esm.svg)](https://npmcharts.com/compare/load-esm?start=365)\n\n# load-esm\n\n**load-esm** is a tiny utility that lets CommonJS (CJS) TypeScript projects **dynamically import pure ESM packages** at runtime—without hacks like `eval()`.\n\nIt helps avoid errors like:\n\n* `Error [ERR_REQUIRE_ESM]: require() of ES Module`\n* `Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No \"exports\" main defined in ...`\n\n---\n\n## Installation\n\n```bash\nnpm install load-esm\n# or\nyarn add load-esm\n# or\npnpm add load-esm\n```\n\n> Works in CJS TypeScript projects. No config changes required.\n\n---\n\n## Quick start\n\n```ts\n// TypeScript (CJS project)\nimport { loadEsm } from \"load-esm\";\n\n(async () => {\n  const esmModule = await loadEsm(\"esm-module\");\n  // use esmModule...\n})();\n```\n\n### With typings\n\n```ts\nimport { loadEsm } from \"load-esm\";\n\n(async () => {\n  const esmModule = await loadEsm<typeof import(\"esm-module\")>(\"esm-module\");\n  // esmModule is fully typed\n})();\n```\n\n### Concrete example (pure ESM package)\n\n```ts\nimport { loadEsm } from \"load-esm\";\n\n(async () => {\n  try {\n    // Import a pure ESM package from a CommonJS TS project\n    const { fileTypeFromFile } = await loadEsm<typeof import(\"file-type\")>(\n      \"file-type\"\n    );\n\n    const type = await fileTypeFromFile(\"fixture.gif\");\n    console.log(type);\n  } catch (error) {\n    console.error(\"Error importing module:\", error);\n  }\n})();\n```\n\n> Note: Because top‑level `await` isn’t available in CommonJS, examples use an async IIFE.\n\n---\n\n## API\n\n```ts\nfunction loadEsm<T = unknown>(name: string): Promise<T>\n```\n\n**Parameters**\n\n* `name` — Package name or file path to import.\n\n**Returns**\n\n* `Promise<T>` resolving to the imported module namespace.\n\n---\n\n## How it works\n\nIn CJS TypeScript projects (`\"module\": \"commonjs\"`), the TS compiler transpiles dynamic `import()` to `require()`, which **breaks** when the target is a pure ESM package.\n\n`load-esm` executes the `import()` **outside of TypeScript’s transpilation scope**, preserving native dynamic `import()` semantics at runtime. This keeps your code type‑safe while avoiding brittle workarounds (e.g., wrapping `import()` in `eval()`).\n\n### What about Node.js ≥ 22.12?\n\nSince Node.js 22.12, `require` can load **some** ESM modules, but there are [documented constraints](https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require). If your dependencies are compatible with that path, you might not need this utility. `load-esm` remains useful when:\n\n* You’re on older Node.js versions that support `import()` (see Compatibility) but not the newer `require()` behavior.\n* You want a single, consistent pattern that works across environments and avoids edge cases.\n\n> If Node’s built‑in `require(esm)` works for your packages and version, feel free to use it.\n\n---\n\n## Compatibility\n\n* **Node.js**: ≥ 13.2.0 (first version with native `import()` support)\n* **TypeScript**: Fully typed; works in CJS projects.\n\n---\n\n## Troubleshooting\n\n* **`ERR_REQUIRE_ESM`**: Ensure you’re using `load-esm(...)` to import the ESM dependency from CJS code.\n* **`No \"exports\" main defined`**: Some packages only expose ESM entry points. Import them via `load-esm`.\n* **Type declarations**: Use the generic form `loadEsm<typeof import(\"pkg\")>(\"pkg\")` for typed access.\n* **Top‑level await**: Wrap usage in an async IIFE in CJS.\n\n---\n\n## License\n\n[MIT](./LICENSE.txt)\n\n---\n\n### Changelog\n\nSee [Releases](https://github.com/Borewit/load-esm/releases).\n\n---\n\n### Acknowledgements\n\nInspired by common pain points when mixing CJS projects with modern ESM‑only libraries.\n","_attachments":{},"homepage":"https://github.com/Borewit/load-esm#readme","bugs":{"url":"https://github.com/Borewit/load-esm/issues"},"license":"MIT"}