TypeChain
TypeChain copied to clipboard
Types cannot be imported when packaged
Discussed in https://github.com/dethcrypto/TypeChain/discussions/755
Originally posted by rtman August 25, 2022 We're trying to make our typechain types reuseable by packaging them up as a private npm package. However this seems to cause issues like the following when in use in nextjs.
./node_modules/@scopeName/packageName/index.ts
Module parse failed: Unexpected token (6:12)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| /* eslint-disable */
> import type * as openzeppelin from "./@openzeppelin";
| import type * as uniswap from "./@uniswap";
| import type * as contracts from "./contracts";
Here's the package.json for the package:
{"name": "@scopeName/packageName", "version": "1.0.0-88d8202", "main": "index.ts"}
The root of the package is the output dir of typechain, containing all the types as well as the copied artifacts dir from hardhat.

I've seen a few related issues to this but no resolution yet https://github.com/dethcrypto/TypeChain/issues/609#issue-1113620678 https://github.com/dethcrypto/TypeChain/issues/278#issuecomment-743359593 , would be great to get some guidance here or a fix.
We're using "@typechain/ethers-v5": "^10.0.0",
having this issue as well.
turns out this is not the correct way to use Typechain. here's what you should do instead:
- include your abis in the npm package
- import that package in nextjs project
- add a step in the nextjs app's build script that runs
typechainon those abis and outputs them into some new folder in the nextjs project. import types from that folder. the folder should be .gitignored.
@jgeary Ok, I can live with just packaging up the abis and then generating types from there. Not ideal imo, but its better than copy pasting abi updates then generating types.
The problem is probably that your tsconfig (or e.g., jest config if the problem happens when running tests) doesn't include typechains types in the ts code that it should target for transpilation (typically one would exclude node_modules). So the solution is to include your package. Here's how to do it so that jest transpiles typechain types from a published package:
// in jest.config.js, or `jest` in package.json, etc.
transformIgnorePatterns: ['<rootDir>/node_modules/(?!(@my-org/my-package)/)'],
// "@my-org/my-package" or simply "my-package" is your package isn't scoped