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

commonjs-esm-loader

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

commonjs-esm-loader

Loads ESM modules on CommonJS projects

1.0.5
latest
Source
npm
Version published
Weekly downloads
558
50%
Maintainers
1
Weekly downloads
 
Created
Source

📚 Introduction

Trying to load a fresh installed package and got the error cannot use import statement outside a module? This packaged is the sollution for your problems.

I've created it after Googling a lot trying to find a sollution on how to import an ESM package in a big and consolidated CommonJS project... Migrate whole project to ESM architecture just to use a single package was not an option.

Is this package usefull to you? Support it buying me a coffee ☕ or giving a ⭐ on GitHub.

🧰 Installation

To install, you can use npm or yarn:

npm install commonjs-esm-loader
# or
yarn add commonjs-esm-loader

💡 Usage

OBS: Using pdfreader package as example because it was the package that makes me create this project, but it works with any ESM package you want to load.

JavaScript

import { importEsmModule } from 'commonjs-esm-loader'

async function myFunction() {
  const { PdfReader } = await importEsmModule( 'pdfreader' )
  // your package is loaded and ready to use :)
  new PdfReader( ... )
}

TypeScript

import { importEsmModule } from 'commonjs-esm-loader'
// load package type so intellisense can work
import type PdfReaderType from 'pdfreader'

async function myFunction() {
  const { PdfReader } = await importEsmModule<typeof PdfReaderType>( 'pdfreader' )
  // your package is loaded, typed and ready to use :)
  new PdfReader( ... )
}

👷 Built With

  • TypeScript - Open-source language which builds on JavaScript

📚 Author

Keywords

commonjs

FAQs

Package last updated on 10 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts