JSPM

commonjs-shim

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 121
  • Score
    100M100P100Q100356F
  • License MIT

Package Exports

  • commonjs-shim
  • commonjs-shim/async

Readme

CommonJS Shim

Shim for the require, __filename, and __dirname globals in Node.js.

Motivation

The require, __filename, and __dirname globals do not exist in ES modules (ESM). If you or any of your dependencies use these globals, you will encounter an error like this:

Error: Dynamic require of "crypto" is not supported

Installation

npm install commonjs-shim
# or
yarn add commonjs-shim
# or
pnpm add commonjs-shim

Usage

This package provides both a sync and async version of this shim. Use the async version if you encounter naming collisions like SyntaxError: Identifier 'createRequire' has already been declared.

Sync

import { commonjsShim } from 'commonjs-shim';

commonjsShim(import.meta.url);

Async

import { commonjsShim } from 'commonjs-shim/async';

await commonjsShim(import.meta.url);

License

MIT