Skip to content

[ERR_UNSUPPORTED_DIR_IMPORT] ES Module factory exports #819

@leovigna

Description

@leovigna

Note: I am uncertain if this is an issue withtypechain or @typechain/ethers-v5. This issue might also exist with @typechain/web3-v1
Recently upgraded typechain in my project to the versions below and I appreciate the modularized exports as we are looking to export typechain types in our contract's npm package. However there is a slight issue when using ESModules due to incorrect exports that do not explicit use the file extensions.
Used versions:

{
"typechain": "8.1.1",
"@typechain/ethers-v5": "10.2.0",
"@typechain/web3-v1": "6.0.2",
}

Below a short example snippet of the typechain generated index.ts when using openzeppelin.

//index.ts
import type * as openzeppelin from "./@openzeppelin";
export type { openzeppelin };
export * as factories from "./factories";
export type { IERC1155Upgradeable } from "./@openzeppelin/contracts-upgradeable/token/ERC1155/IERC1155Upgradeable";
export { IERC1155Upgradeable__factory } from "./factories/@openzeppelin/contracts-upgradeable/token/ERC1155/IERC1155Upgradeable__factory";

We then export these types in our package so developers can easily use them.

export * as TypechainEthers from "./typechain/ethers/index.js"

There is a slight issue when using ESModules. When using ESModules, js files MUST be imported with explicit file name extensions. However this only applies to importing variables and NOT types in TS files (as these have no runtime value). Therefore in the snippet above, while the contract types are properly exported, the factory exports (both factories and IERC1155Upgradeable__factory) don't just export types but rather a class that mimics ethers factory but with proper types. Therefore the import breaks because the factory is not exported with explicit reference to the .js file. Here would be an example fix to the above snippet.

//index.ts
import type * as openzeppelin from "./@openzeppelin";
export type { openzeppelin };
export * as factories from "./factories/index.js"; //add "/index.js"
export type { IERC1155Upgradeable } from "./@openzeppelin/contracts-upgradeable/token/ERC1155/IERC1155Upgradeable";
export { IERC1155Upgradeable__factory } from "./factories/@openzeppelin/contracts-upgradeable/token/ERC1155/IERC1155Upgradeable__factory.js"; //add ".js"

Please let me know if you have an idea where the logic for the export file generation is and I would be open to looking into making the patch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions