expo-file-system File.write() is async in Expo SDK 58
Expo SDK 58 changed the File class of expo-file-system. The release notes list it under breaking changes: "File.write() is now async and returns a promise; use File.writeSync() for the previous behavior." They also deprecate File.md5 in favour of File.digest().
Why it is easy to miss. In SDK 57, write() returned void; in SDK 58 it returns Promise<void>. Calling a function that returns a promise without await is still valid TypeScript, so the project compiles and nothing warns you. What changes is behaviour:
- The next line can run before the file is written. Code that writes and then reads, uploads, shares or hashes the file in the same function can see the old content, or no file at all, depending on timing.
- Errors escape your try/catch. A failing write (full disk, a bad path) used to throw where you called it. Now it rejects the promise; if nobody awaits it, the
try/catcharound it never sees the error and it surfaces as an unhandled promise rejection.
Fix. Await the write, or keep the old synchronous behaviour where you depend on it:
const file = new File(Paths.document, 'cache.json');
- file.write(JSON.stringify(data));
- upload(file.uri);
+ await file.write(JSON.stringify(data));
+ upload(file.uri);
// or, where the call must stay synchronous:
+ file.writeSync(JSON.stringify(data));
For checksums, File.md5 still works but is deprecated. Its replacement is a promise too, and supports more algorithms (MD5, SHA-1, SHA-256, SHA-384, SHA-512):
- const hash = file.md5;
+ const hash = await file.digest('MD5');
To find calls that drop a promise anywhere in the project, the @typescript-eslint/no-floating-promises lint rule reports them.
Check before you upgrade. npx nativekeel upgrade lists the files that write with the expo-file-system File class or read .md5 when your app is below Expo SDK 58, with the other breaking changes between your SDK and the newest one. In our scan of 947 open-source React Native apps, 65 that are still below SDK 58 use them.
npx nativekeel