-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Dual emitting using tshy
Maor Leger edited this page Aug 12, 2025
·
2 revisions
The Azure SDK for JS client libraries are built for both CommonJS and ESM.
While the two formats are largely compatible, a few caveats apply.
When a module is require
d in commonJS, __dirname and __filename are available via the module system; however, in ESM the same globals* are not available.
Likewise, in CommonJS the globals import.meta.*
are not available for usage.
Our build tool, isaacs/tshy provides a mechanism for automatically polyfilling the correct syntax depending on the import type.
In order to properly fetch the current filename or current dirname you could do the following:
// myFile.ts - ESM flavor, use import.meta
const currentDir = dirname(fileURLToPath(import.meta.url));
// myFile-cjs.cts - CommonJS flavor, use __dirname and __filename
const currentDir = __dirname
Note that the file naming convention is important! The commonJS polyfill must:
- begin with the same file name as the ESM flavor
- Add the
-cjs
postfix to the file name - Use
.cts
extension