-
Notifications
You must be signed in to change notification settings - Fork 299
Open
Labels
Description
Environment Details
- OS: macOS / Linux (Docker)
- Node Version: 22.14+
- Package Manager: pnpm 10.17.1
- BitGoJS Version:
@bitgo/[email protected],@bitgo/[email protected] - BitGo Environment: Both testnet and mainnet
Expected Behavior
ESM builds in dist/esm/ should use proper Node.js ESM-compatible imports with explicit .js file extensions for relative imports, allowing the packages to be consumed in ESM projects.
Current Behavior
ESM builds use relative imports without .js extensions, causing ERR_MODULE_NOT_FOUND errors when importing the packages in Node.js ESM environments.
Error 1 (@bitgo/abstract-utxo):
Error [ERR_MODULE_NOT_FOUND]: Cannot find module
'/app/node_modules/@bitgo/abstract-utxo/dist/esm/abstractUtxoCoin'
imported from '/app/node_modules/@bitgo/abstract-utxo/dist/esm/index.js'
Error 2 (@bitgo/utxo-core):
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'bip174/src/lib/utils'
imported from '/app/node_modules/@bitgo/utxo-core/dist/esm/paygo/psbt/payGoAddressProof.js'
Possible Solution
The TypeScript compilation for ESM output needs to add .js extensions to all relative imports. This can be achieved by:
- Using
tsc-aliasortsconfig-pathspost-processing - Setting
moduleResolution: "NodeNext"with proper TypeScript configuration - Using a bundler plugin like
rollup-plugin-add-import-extension
Example of broken vs fixed imports:
// Current (broken)
export * from './abstractUtxoCoin';
import { foo } from './address';
import { checkForOutput } from 'bip174/src/lib/utils';
// Expected (fixed)
export * from './abstractUtxoCoin.js';
import { foo } from './address/index.js';
import { checkForOutput } from 'bip174/src/lib/utils.js';Steps to Reproduce
- Create an ESM Node.js project (
"type": "module"in package.json) - Install
@bitgo/[email protected](which depends on@bitgo/[email protected]) - Import the package:
import { Zec } from '@bitgo/sdk-coin-zec';
- Run with Node.js 22+:
node index.js
- Observe
ERR_MODULE_NOT_FOUNDerror
Affected Versions
| Package | Last Working | Broken |
|---|---|---|
@bitgo/abstract-utxo |
10.6.0 (CJS only) | >=10.7.0 (ESM introduced) |
@bitgo/utxo-core |
1.23.0 (CJS only) | >=1.24.0 (ESM introduced) |
Workaround
We are currently using pnpm patches to add .js extensions to all ESM imports:
{
"pnpm": {
"patchedDependencies": {
"@bitgo/[email protected]": "patches/@[email protected]",
"@bitgo/[email protected]": "patches/@[email protected]"
}
}
}This is not sustainable as patches need to be regenerated for each version upgrade.