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 supportedInstallation
npm install commonjs-shim
# or
yarn add commonjs-shim
# or
pnpm add commonjs-shimUsage
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