Shim for the require
, __filename
, and __dirname
globals in Node.js.
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
npm install commonjs-shim
# or
yarn add commonjs-shim
# or
pnpm add commonjs-shim
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
.
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.
import { commonjsShim } from 'commonjs-shim';
commonjsShim(import.meta.url);
import { commonjsShim } from 'commonjs-shim/async';
await commonjsShim(import.meta.url);
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',
})
MIT