TypeChain: Cannot find name 'artifacts', 'contract' after upgrading to typechain v2 [truffle v5]

Hey, First and foremost, thank you so much for everyone’s work here. It’s great and extremely helpful.

I’ve built a dapp boilerplate and I’ve been upgrading it recently to have something a bit different. Throughout that, I’ve upgraded typechain to v2, and all the other dependencies attached to it. But because of that, I now get some errors as described below.

Could you guys help me, please? I’ll be very thankful.

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.

TSError: ⨯ Unable to compile TypeScript:
test/SimpleStorage.test.ts:4:23 - error TS2304: Cannot find name 'artifacts'.

4 const SimpleStorage = artifacts.require('SimpleStorage');
                        ~~~~~~~~~
test/SimpleStorage.test.ts:8:1 - error TS2304: Cannot find name 'contract'.

8 contract('SimpleStorage', (accounts) => {
  ~~~~~~~~
test/SimpleStorage.test.ts:8:28 - error TS7006: Parameter 'accounts' implicitly has an 'any' type.

8 contract('SimpleStorage', (accounts) => {
                             ~~~~~~~~

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (4 by maintainers)

Most upvoted comments

The way I fixed this was to use the tsconfig.json file here

https://github.com/ethereum-ts/truffle-typechain-example/blob/master/tsconfig.json

And then I changed truffle-contracts to truffle-typings, under types.

You will need the truffle-typings package installed in your project.

Having the exact same issue…

I had a similar issue with another project using jest, and the solution was something along the lines of adding the global libraries in the jest config file.

The funny thing in my situation is that the linters in my IDE are perfectly happy… They get all the types fine, autocomplete and everything.

But when I run truffle test all of the assumed global variables dealing with truffle / the generated types are forgotten apparently.

For anyone else with this problem, I solved it by including Typechain’s output folder in the Typescript config file tsconfig.json. By default, that’s the typechain folder. It can also be set with Typechain’s --outDir option.

"include": [
    "./test",
    "./typechain"
  ],

I have a HardHat project using Truffle tests, so my full tsconfig.json file is

{
  "compilerOptions": {
    "target": "es2018",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "outDir": "dist"
  },
  "include": [
    "./test",
    "./typechain"
  ],
  "files": ["./hardhat.config.ts"]
}

FYI, my HardHat config in hardhat.config.ts is

import "@nomiclabs/hardhat-truffle5"
import "hardhat-typechain"

export default {
  solidity: "0.7.6",
  typechain: {
    target: "truffle-v5",
  },
};