ESM modules are becoming a norm these days with some of the popular modules published as only ESM modules(example got, ky). It also comes with a lot of useful features like Top-level await, etc.
But, there are a few features that CommonJS supports but ESM doesnt like these. It becomes a pain to migrate each of these features to a ESM compatible feature and its a lot of manual work. This polyfill tries to support those commonly used and feasible features in ESM modules.
- __filename
- __dirname
- require
- require.resolve
$ npm install --save @lordofthethings/esm-polyfills
$ yarn add @lordofthethings/esm-polyfills
$ node -r @lordofthethings/esm-polyfills <your-script>.mjs
Then in your <your-script>.mjs
file, globals like __filename
, __dirname
, require
and require.resolve
will be automatically available:
console.log('__filename', __filename);
console.log('__dirname', __dirname);
console.log(require('lodash'), require.resolve('lodash'), require('../package.json'));
If you dont want to auto polyfill, then you can programmatically import them and use it your code like this:
$ ESM_POLYFILLS_GLOBAL=false node -r @lordofthethings/esm-polyfills <your-script>.mjs
Then in your <your-script>.mjs
file, do:
import { getFileName, getDirName, getRequire as _require_ } from '@lordofthethings/esm-polyfills';
const __filename = getFileName();
const __dirname = getDirName();
console.log('__filename', __filename);
console.log('__dirname', __dirname);
console.log(_require_('lodash'), _require_.resolve('lodash'));