blockchainsmartcontractssolanaanchor-solana

Solana Anchor Lang: anchor init creates project with TypeError: Cannot read properties of undefined (reading 'methods')


Confused about what could have caused this to stop working, I installed solana and anchor and was able to test projects. anchor --version anchor-cli 0.26.0

nvm use 16.16.0
Now using node v16.16.0 (npm v8.11.0)
anchor init hello-world
yarn install v1.22.19
warning package.json: No license field
info No lockfile found.
warning No license field
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
Done in 6.07s.
Initialized empty Git repository in /home/user/Code/anchor-second-test/hello-world/.git/
hello-world initialized

cd hello-world && anchor build works. Making no changes, I run anchor run test

1) hello-world
       Is initialized!:
     TypeError: Cannot read properties of undefined (reading 'methods')
      at /home/user/Code/anchor-second-test/hello-world/tests/hello-world.ts:13:30
      at Generator.next (<anonymous>)
      at /home/user/Code/anchor-second-test/hello-world/tests/hello-world.ts:31:71
      at new Promise (<anonymous>)
      at __awaiter (tests/hello-world.ts:27:12)
      at Context.<anonymous> (tests/hello-world.ts:11:36)
      at processImmediate (node:internal/timers:466:21)



error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Here is hello-world.ts test file as generated:

import { Program } from "@project-serum/anchor";
import { HelloWorld } from "../target/types/hello_world";

describe("hello-world", () => {
  // Configure the client to use the local cluster.
  anchor.setProvider(anchor.AnchorProvider.env());

  const program = anchor.workspace.HelloWorld as Program<HelloWorld>;

  it("Is initialized!", async () => {
    // Add your test here.
    const tx = await program.methods.initialize().rpc();
    console.log("Your transaction signature", tx);
  });
});

I have tried using node lts 16.16.0 instead of the latest lts, starting in a clean directory, checking that typescript was installed, running yarn install before running anchor build command. I checked that the solana keypair and json file was generated in the correct folder.


Solution

  • Go to this ../target/types/hello_world directory and make sure it exports like this

    export type HelloWorld = {}
    

    since you have this import

    import { HelloWorld } from "../target/types/hello_world";
    

    also, you did not show anchor import . It should be

    import * as anchor from "@project-serum/anchor";