Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types cannot be imported when packaged #758

Open
rtman opened this issue Aug 26, 2022 Discussed in #755 · 4 comments
Open

Types cannot be imported when packaged #758

rtman opened this issue Aug 26, 2022 Discussed in #755 · 4 comments

Comments

@rtman
Copy link

rtman commented Aug 26, 2022

Discussed in #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.

Screen Shot 2022-08-25 at 10 28 35 AM

I've seen a few related issues to this but no resolution yet #609 (comment) #278 (comment) , would be great to get some guidance here or a fix.

We're using "@typechain/ethers-v5": "^10.0.0",

@jgeary
Copy link

jgeary commented Sep 13, 2022

having this issue as well.

@jgeary
Copy link

jgeary commented Sep 15, 2022

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 typechain on those abis and outputs them into some new folder in the nextjs project. import types from that folder. the folder should be .gitignored.

@rtman
Copy link
Author

rtman commented Sep 15, 2022

@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.

@sebastiendan
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants