coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.38k stars 1.26k forks source link

Cannot read properties of undefined (reading '.js') #2911

Closed hoseadevops closed 2 months ago

hoseadevops commented 2 months ago

solana-anchor-hello-world git:(main) ✗ anchor test Compiling solana-anchor-hello-world v0.1.0 (/Users/hosea/owner/devops/contract/solana/solana-anchor-hello-world/programs/solana-anchor-hello-world) Finished release [optimized] target(s) in 0.52s Compiling solana-anchor-hello-world v0.1.0 (/Users/hosea/owner/devops/contract/solana/solana-anchor-hello-world/programs/solana-anchor-hello-world) Finished test profile [unoptimized + debuginfo] target(s) in 0.35s Running unittests src/lib.rs (/Users/hosea/owner/devops/contract/solana/solana-anchor-hello-world/target/debug/deps/solana_anchor_hello_world-1eca08133de79c9c)

Found a 'test' script in the Anchor.toml. Running it as a test suite!

Running test suite: "/Users/hosea/owner/devops/contract/solana/solana-anchor-hello-world/Anchor.toml"

(node:8673) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time (Use node --trace-warnings ... to show where the warning was created) [ERROR] Cannot read properties of undefined (reading '.js')

hoseadevops commented 2 months ago

anchor init solana-anchor-hello-world cd solana-anchor-hello-world

anchor build

anchor test

hoseadevops commented 2 months ago

anchor --version anchor-cli 0.30.0 solana --version solana-cli 1.18.11 (src:c78a9720; feat:3469865029, client:SolanaLabs)

hoseadevops commented 2 months ago

tsconfig.json's typeRoots references the node_modules directory

bug node_modules directory is missing after successful anchor init/yarn install

Adding a .yarnrc.yml file to the project root directory with the following content solves this issue for me:

nodeLinker: node-modules

Reference

taivo commented 1 month ago

I just ran into this error message (May 2024). I was running yarn v3.1.1. Upgraded to 4.x via the command yarn set version stable resolved the error above without making any changes to the tsconfig.json generated by anchor init.