I rewrote my app's animation layer because I misread one error message
I spent a long day concluding that react-native-reanimated was incompatible with React Native 0.83.1. I then rewrote an entire app's animation layer onto React Native's built-in Animated to work around it.
The library was never incompatible. I had misread the error message, and the error message deserves a good share of the blame. Here is the whole thing, including the part where a maintainer told me I was wrong about the fix too, and was right.
The symptom
React Native 0.83.1, New Architecture, Hermes. react-native-reanimated@4.5.3 with react-native-worklets@0.11.3. The app built, installed, launched - and died before a single screen rendered:
TypeError: Cannot read property 'bytecode' of undefined at installUnpackers (react-native-worklets/.../NativeWorklets.native.ts)
No app code of mine was in the stack. The crash was inside the library's own bootstrap.
Why I concluded "incompatible"
Read that error as a working developer under time pressure. It says bytecode. Hermes compiles to bytecode. The crash is inside the library's startup, before my code runs. The obvious story writes itself: this library's native/JS layers disagree on this version of React Native.
That story was reinforced by a real data point. react-native-reanimated@3.19.5 genuinely cannot build on RN 0.83 - it calls UIManagerModuleListener, which was removed from core. I had already hit that. So "Reanimated has version problems on 0.83" was not a wild guess; it was a pattern I had just observed.
I bumped patch versions. I did clean native rebuilds. I analysed the bundle. Nothing moved. So I made the pragmatic call: rip out Reanimated, rebuild the animation layer on built-in Animated + LayoutAnimation + PanResponder, ship the app.
That was the right shipping decision. It was based on a wrong diagnosis.
Coming back with a clean repro
Weeks later I rebuilt the situation from zero - stock template, nothing else:
npx @react-native-community/cli init Repro --version 0.83.1
npm i react-native-reanimated@4.5.3 react-native-worklets@0.11.3
plus the one line the docs tell you to add:
// babel.config.js
plugins: ['react-native-worklets/plugin'],
It worked. First try. Text faded in, worklet ran, no crash. So RN 0.83.1 was never the blocker. Something about my setup was - and that one Babel line turns out to be the entire story.
The actual mechanism
installUnpackers() starts like this:
const value = (installValueUnpacker as WorkletFunction).__initData!;
// ...
if (value.bytecode !== undefined) { // <- the throw
And installValueUnpacker ships inside the package looking like this:
export function installValueUnpacker() {
'worklet';
// ...
}
Notice what is not there: __initData. The library ships the function with a 'worklet' directive and nothing else. Your Babel plugin is what attaches __initData - including onto the library's own code inside node_modules.
If that transform doesn't run, __initData is undefined. That ! is a TypeScript non-null assertion; it compiles to nothing. There is no runtime check. So value is undefined, value.bytecode throws, and the message you get names bytecode - a word that appears nowhere near the actual problem.
The error points at Hermes. The cause is Babel.
A 30-second diagnosis
You don't need a device, an emulator, or a native build to check this. Bundle offline and count the injected metadata:
npx react-native bundle --platform android --dev true \
--entry-file index.js --bundle-output /tmp/b.js --reset-cache
grep -c "installValueUnpacker\.__initData *=" /tmp/b.js
| babel.config.js | installValueUnpacker.__initData = | total __initData = |
|---|---|---|
| plugins: ['react-native-worklets/plugin'] | 1 | 478 |
| plugin removed | 0 | 2 |
Zero assignments means __initData will be undefined at runtime. That's your answer, in about as long as a bundle takes.
Two notes that cost me time:
- In Reanimated 4 the transform lives in
react-native-worklets/plugin. The oldreact-native-reanimated/pluginstill works - it's literally a four-line re-export - so either name is fine. - A reload is not enough after changing Babel config. Metro keeps its Babel workers alive; you need
--reset-cache.
The answer was shipped in the package the whole time
Here is the part that stung. Both packages ship a compatibility.json:
// react-native-reanimated/compatibility.json
"4.5.x": {
"react-native": ["0.83", "0.84", "0.85", "0.86"],
"react-native-worklets": ["0.10.x", "0.11.x"]
}
Reanimated's goes back to RN 0.63 and is split by architecture. One cat of a file already sitting in my node_modules would have told me that 4.5.3 on 0.83 is supported, and that my "version incompatibility" theory was wrong before I rewrote anything. It also correctly records 3.19.x as RN 0.78-0.81 - so the one real incompatibility I hit was documented too. I just never looked.
Taking it upstream, and being wrong again
I filed an issue arguing installUnpackers should guard that read and say what actually went wrong, with a minimal repro. Maintainer tjzel replied: Worklets already has a check for a missing transform, and could I work out why it hadn't fired?
It does have one, and it's a good one - a __DEV__ self-test that creates a tiny worklet and throws a clear [Worklets] Failed to create a worklet if the transform didn't take. But look at the ordering in init():
initializeRuntime(); // unpacker installation
// ...
initializeRNRuntime(); // the self-test lives in here
initializeRuntime() installs the unpackers. Doing that is the first thing to require the NativeWorklets module, which instantiates at module scope - export const WorkletsModule = new NativeWorklets() - and that constructor calls installUnpackers(). It throws there, so control never reaches initializeRNRuntime() at all - and the self-test inside it never runs.
So the guard is correct and simply runs a step too late. I moved the existing self-test above initializeRuntime() and measured it on the repro, with the Babel plugin still missing:
| matching logcat lines | before | after |
|---|---|---|
| Cannot read property 'bytecode' of undefined | 6 | 0 |
| [Worklets] Failed to create a worklet | 0 | 6 |
(Both errors get logged several times per launch, so read those as present versus absent.)
Their own error, firing properly. The guard I'd originally proposed turned out to be unnecessary - the fix is a reorder, smaller than what I'd asked for. Following the maintainer's objection produced a better patch than my issue did. It merged, and ships in react-native-worklets 0.15.0. So the next person who forgets that Babel line gets told about the Babel line.
One trap while testing that: the package sets "react-native": "./src/index", so Metro bundles src/, not lib/module/. My first patch did nothing at all because I edited the compiled output. I only caught it by grepping the served bundle for my own change and finding zero hits.
What I'd tell myself
An error message names the line that threw, not the thing that's wrong. bytecode was where the crash surfaced. Babel was the cause. I treated the first as a diagnosis.
Check what's already in node_modules. A compatibility.json sitting in the package I was arguing with would have ended the investigation on day one.
Verify the experiment before trusting its result. My first patch "failed" and almost sent a wrong answer to a maintainer. It hadn't failed - it had never run.
A workaround is not a diagnosis. Rewriting the animation layer was a sound call under a deadline, and I'd make it again. But I filed it in my head as "Reanimated doesn't work on 0.83," and that note was wrong for weeks.
I still haven't proven what specifically broke my original build - the config from that day is gone. The most likely candidate is that the emulator was loading a different project's Metro bundle from port 8081, which would carry no worklets transform at all and produce exactly this crash. But I can't demonstrate it, so I won't claim it. What I can demonstrate is that the conclusion I acted on was wrong, and that one grep would have shown me.
Comments
No comments yet. Start the discussion.