commonjs-shim

1.1.0 • Public • Published

CI npm

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 synchronous and asynchronous version of this shim. Use the asynchronous version if you encounter naming collisions like SyntaxError: Identifier 'createRequire' has already been declared.

Shim

If you want to make the globals available in your own code, you can do so by importing the commonjsShim function and calling it with the import.meta.url argument.

Synchronous

import { commonjsShim } from 'commonjs-shim';

commonjsShim(import.meta.url);

Async

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

await commonjsShim(import.meta.url);

Banner

If you want to inject the shim into your bundled code, you can use the commonjsBanner template string. For example, esbuild provides a banner option that can be used to insert an arbitrary string at the beginning of generated JavaScript file:

import { commonjsBanner } from 'commonjs-shim'; // or 'commonjs-shim/async'
import * as esbuild from 'esbuild';

await esbuild.build({
  entryPoints: ['app.js'],
  banner: {
    js: commonjsBanner,
  },
  outfile: 'out.js',
})

License

MIT

Package Sidebar

Install

npm i commonjs-shim

Weekly Downloads

152

Version

1.1.0

License

MIT

Unpacked Size

7.86 kB

Total Files

7

Last publish

Collaborators

  • chriszirkel