Hi everyone,
I’m encountering an odd behavior with the @capacitor/filesystem plugin (v7.0.0) in my Android app, and I’d love some insights from the community on why this is happening and how to align it with the “correct” approach.
Context
I’m loading the Filesystem plugin via a tag in my HTML:
html
<script src="https://unpkg.com/@capacitor/filesystem@latest/dist/filesystem.js"></script>
My app is built as a single-file JavaScript project (no bundler like Vite or Webpack) and runs natively on Android using Capacitor. I’m using the Filesystem API to save and load data, like this:
javascript
await Filesystem.writeFile({
path: 'diario-data.json',
data: JSON.stringify({ tema: 'padrao' }),
directory: "DATA", // String literal
encoding: "utf8"
});
await Filesystem.readFile({
path: 'diario-data.json',
directory: "DATA",
encoding: "utf8"
});
What Works
Using directory: “DATA” (string literal) works perfectly on Android. Logs from adb logcat show successful writes and reads, and the file is saved in what I assume is the app’s private data directory (e.g., file:///data/data/com.myapp/files/diario-data.json). I get no errors, and my app behaves as expected.
The Strange Part
According to the Filesystem API docs for v7.0.0, directory should be a value from the Directory enum (e.g., Directory.DATA), not a string literal. However:
- When I log Directory.DATA and Encoding.UTF8, they are undefined:
javascript
console.log("Directory.DATA:", Directory.DATA); // undefined
console.log("Encoding.UTF8:", Encoding.UTF8); // undefined
console.log("Directory:", Directory); // [object Object]
console.log("Encoding:", Encoding); // [object Object]
- If I try to use Directory.DATA instead of “DATA”, I get errors because it’s undefined, and the operation fails.
What I’ve Tried
-
Initially used “data” (lowercase), which failed with Invalid directory.
-
Switched to “DATA” (uppercase), which worked consistently.
-
Checked the loading — it seems to load fine (window.Capacitor.Plugins.Filesystem exists), but the enums are missing.
Questions
-
Why does directory: “DATA” work as a string literal when the docs say it should be Directory.DATA? Is this an undocumented feature or a bug in the Android native plugin?
-
Has anyone else faced Directory.DATA and Encoding.UTF8 being undefined when loading via ? Is this a limitation of the unpkg distribution or my setup?
-
Why doesn’t the “correct” way (Directory.DATA) work in my case, even with v7.0.0? Shouldn’t the enums be available globally when the plugin is loaded this way?
-
How can I make the enums work properly without switching to a full npm/bundler setup (which I’d like to avoid for now)?
Logs
Here’s a snippet from adb logcat showing it working with “DATA”:
2025-03-06 10:36:03.128 Iniciando salvamento no Filesystem...
2025-03-06 10:36:03.301 Resultado do salvamento: [object Object]
2025-03-06 10:36:03.301 Dados salvos com sucesso no Filesystem: padrao
Environment
-
Capacitor: v7.0.0 (via unpkg.com/@capacitor/filesystem@latest)
-
Platform: Android
-
No npm/bundler, just a single HTML/JS file with Capacitor CLI for native build
Any insights on why this workaround works, why the enums are missing, or how to use the enums correctly in this setup would be greatly appreciated! Thanks!
P.S. I’m using AI to generate this question cause I’m Brazilian and I don’t speak english.