This debugging story is from the do-not-stop project - a full-stack Web3 app with React frontend, React Native mobile frontend, Node.js backend, and multi-blockchain smart contracts.
The Mystery Begins
It all started with a crash in @reown/appkit-react-native. While debugging that crash, I noticed something weird happening with valtio. The subscribe() function seemed to work fine, but proxy() wasn't behaving as expected. I decided to dig into the library code itself and added some debugging directly in node_modules/valtio/vanilla.js.
I printed proxyStateMap.get(proxyObject) in both functions, and that's when things got interesting:
- In
proxy(): Got the correct value ✓ - In
subscribe(): Gotundefined✗
Wait, what? They were completely different WeakMap instances from different valtio module instances! The proxy object was being stored in one proxyStateMap, but when subscribe() tried to find it, it was looking in a completely different proxyStateMap from a different valtio instance. That's like putting your keys in one drawer and looking for them in another.
A Quick Note on Debugging Library Code
Before I continue, here's something I had to remember: when you're adding console.log statements to library code in node_modules, your logs might not show up because modules get cached. I spent way too long wondering why my logs weren't appearing before I remembered to run:
pnpm start --reset-cache
Adding logs directly to library code is actually super helpful when you're dealing with weird behavior - you can see exactly what's happening inside the library, trace the execution flow, and peek at internal state that's not exposed through the public API. Just don't forget to clear that cache!
Going Back in Time
At this point, I was stuck. The code was working before, so what changed? I did what I should have done from the start: I checked git history.
I went through past commits one by one until I found a commit where everything worked. Then I compared the diff between the working state and the broken state. Bingo - there it was, a single line change in .npmrc:
+ node-linker=hoisted
That one line was the culprit.
What Actually Happened
After adding node-linker=hoisted, pnpm switched to a flat structure similar to npm, and that broke the sharing mechanism by creating separate valtio instances:
node_modules/valtio (version 1.13.2)
node_modules/@reown/appkit-core-react-native/node_modules/valtio (version 2.1.8)
node_modules/@reown/appkit-react-native/node_modules/valtio (version 2.1.8)
Why did this happen? A few reasons:
-
Version mismatch: The root had
1.13.2(some transitive dependency), while the nested packages needed2.1.8 -
No virtual store: With
node-linker=hoisted, pnpm doesn't use the.pnpmvirtual store structure that ensures proper symlinking - pnpm kept them separate to avoid conflicts
Since they were using different valtio instances, and each instance had its own proxyStateMap WeakMap, the proxy object stored in one proxyStateMap wasn't found when subscribe() looked in a different proxyStateMap.
The Fix
The solution was to force all valtio dependencies to use the same version. I added this to my root package.json:
{
"pnpm": {
"overrides": {
"valtio": "2.1.8"
}
}
}
Then ran pnpm install, and that was it. This:
- Unified all valtio to
2.1.8 - Allowed pnpm to hoist it to the root
- Removed the nested instances
- Made all packages share the same
proxyStateMap
Alternatively, I could have just removed node-linker=hoisted to go back to pnpm's default virtual store, which handles this automatically. But I needed the hoisted structure for other reasons, so the override approach worked better for me.
Key Takeaways
Check git history first - When something breaks, see what changed. The diff often reveals the root cause immediately.
Adding console.log to library code is a good debugging habit - When you encounter weird library behavior, adding logs directly in
node_modulescan reveal exactly what's happening inside the library. Just remember to use--reset-cachewhen debugging library code innode_modulesto see your changes.Multiple package instances can break singleton-like behavior - Libraries that rely on WeakMaps, module-level state, or singletons can fail when multiple instances exist. This isn't just a valtio thing - it can happen with any library that maintains internal state.
Version mismatches prevent hoisting - Even with
node-linker=hoisted, pnpm will keep separate instances if versions differ. Usepnpm.overridesto force consistent versions across your workspace.
Happy debugging!
Top comments (0)