@uscreen.de/common-esm
make ESM a bit more commonJS
ESM still lacks some features that are "common" in commonJS. This package aims to fill that gap.
Prior art
-
The desm package already provides us with dirname
, filename
, join
. Straight simple and robust. This package aims to decorate it with some more commonJS features.
-
And there is esm-utils which provides a bigger api surface. But I found its codebase a bit too complex and it does not provide a solution for if (require.main === module)
, either.
Install
npm install @uscreen.de/common-esm
Usage
The default export is a function that returns an object with "common" commonJS properties and functions that depend on the import.meta.url
:
import CommonESM from '@uscreen.de/common-esm'
const { __dirname, __filename, __isMain, join, requireJson } = new CommonESM(import.meta.url)
console.log(__dirname)
console.log(__filename)
console.log(__isMain)
console.log(join('foo', 'bar'))
console.log(requireJson('./package.json'))
Inherited from desm there are the following functional exports:
import { dirname, filename, join, requireJson, isMain } from '@uscreen.de/common-esm'
console.log(dirname(import.meta.url))
console.log(filename(import.meta.url))
console.log(join(import.meta.url, 'routes'))
console.log(join(import.meta.url, '..', 'other'))
console.log(requireJson(import.meta.url, './package.json'))
console.log(isMain(import.meta.url))
License
Licensed under MIT.
Published, Supported and Sponsored by u|screen