🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

load-esm

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

load-esm - npm Package Compare versions

Comparing version

to
1.0.1

index.js

21

package.json
{
"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"
}
}

24

README.md

@@ -0,1 +1,2 @@

[![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)
[![NPM version](https://img.shields.io/npm/v/load-esm.svg)](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`.