Tldr:
Why am I unable to use
const fs = await import("node:fs/promises");
In my plugin code?
Details:
I have built an Obsidian plugin using typescript, it works great. I’m now trying to add additional functionality, and am adding an additional external package that adds some functionality (PackageX).
Internally, within the code of that package (PackageX, which I cannot change), there is code like so:
const fs = await import("node:fs/promises");
This is a dynamic import, using the module ‘node:’ namespace. And this is valid syntax, but a recent addition, I think.
As soon as I try to use this external package, the build of my plugin fails with this error:
Could not resolve "node:fs/promises". You can mark the path "node:fs/promises" as external to exclude it from the bundle, which will remove this error. You can also add ".catch()" here to handle this failure at run-time instead of bundle-time.
I can indeed add “node.*” to my esbuild.config.mjs under externals as indicated above, and this resolves the build-time error, but then instead at runtime of the plugin, I get this error:
Uncaught (in promise) TypeError: Failed to fetch dynamically imported module: node:fs/promises
This implies that when Obsidian is running, it doesn’t recognize (or use) the “node:” prefix and is unable to pull “fs/promises” from the built-in (local?) node packages.
It’s not specifically that external PackageX I’m trying to pull in, either. If I uninstall that PackageX entirely, and simply attempt to use await import(“node:fs/promises”) directly in my own plugin, it fails in the same way. I only mention PackageX because I can’t change the code and do it another way if I want to use it.
I’m relatively ok with typescript, but I can’t really unravel what is needed here. I’m using more or less the “standard” package.json, esbuild.config.mjs, tsconfig.json you see in a lot of plugins. I suspect perhaps I need to add something, or change the esbuild target, or…?
Anyone have any clues to offer, workarounds, seen this before? And also, can I be confirmed that the “platform” of esbuild here should be “browser”, not “node”?
Much appreciated.