Comparing version
{ | ||
"name": "load-esm", | ||
"version": "1.0.0", | ||
"main": "index.cjs", | ||
"version": "1.0.1", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
"test": "mocha", | ||
"compile-test": "tsc -p test" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"author": { | ||
@@ -48,3 +56,10 @@ "name": "Borewit", | ||
}, | ||
"dependencies": {} | ||
"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" | ||
} | ||
} |
@@ -0,1 +1,2 @@ | ||
[](https://github.com/Borewit/load-esm/actions/workflows/nodejs-ci.yml) | ||
[](https://npmjs.org/package/load-esm) | ||
@@ -38,6 +39,13 @@ | ||
To import the typings you need do the following: | ||
```ts | ||
import {loadEsm} from 'load-esm'; | ||
(async () => { | ||
const esmModule = await loadEsm<typeof import('esm-module')>('esm-module'); | ||
})(); | ||
``` | ||
A concrete example loading [file-typ](https://github.com/sindresorhus/file-type), a pure ESM package: | ||
```ts | ||
import * as path from 'path'; | ||
import {loadEsm} from 'load-esm'; | ||
@@ -50,4 +58,4 @@ | ||
try { | ||
// Dynamically import the ESM module | ||
const { fileTypeFromFile } = await loadEsm('file-type'); | ||
// Dynamically import the ESM module, including types | ||
const { fileTypeFromFile } = await loadEsm<typeof import('file-type')>('file-type'); | ||
@@ -75,13 +83,3 @@ // Use the imported function | ||
- A `Promise<T>` that resolves to the imported module object. | ||
### Example | ||
```ts | ||
import { loadEsm } from 'load-esm'; | ||
(async () => { | ||
const module = await loadEsm('some-esm-module'); | ||
console.log(module); | ||
})(); | ||
``` | ||
## How It Works | ||
@@ -88,0 +86,0 @@ Using `await import` in a CommonJS TypeScript project poses challenges because the TypeScript compiler transpiles `import()` statements to `require()` calls when module is set to CommonJS in `tsconfig.json`. |
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
5676
8.74%6
Infinity%98
-2%