New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

commonjs-esm-loader

Package Overview
Dependencies
Maintainers
1
Versions
4
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.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
173
decreased by-27.62%
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.

🧰 Installation

To install, you can use npm or yarn:

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

💡 Usage

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

import { importEsmModule } from 'commonjs-esm-loader'
import type PdfReaderType from 'pdfreader'

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

If you're using Typescript, it's important to import add import type PackageType from 'mypackage' so that intellisense will continue to work.

👷 Built With

  • TypeScript - Open-source language which builds on JavaScript

📚 Author

Keywords

FAQs

Package last updated on 06 Nov 2023

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc