Plugin - esBuild - Platform = Node vs Browser

In the various samples out there, the typical esBuild config (esbuild.config.mjs) shows a build with platform being blank (unspecified). Per the esbuild specification, the platform value can be either “browser” or “node”, but if unspecified, defaults to “browser”. In other words, if esbuild.config.mjs doesn’t specify, then the build uses “browser” and the plugin is built as though it is running in a browser, not a node application. In most cases, I think this is just fine.

Interestingly, this note from this own forum indicates (from @davecan ) that actually, an Obsidian plugin is not strictly running as a browser, but is running as an elevated node process under the user’s credentials, within Chromium/Electron. So, while “browser” works, a build should more accurately be platform=“node” in some cases.

I ran into this distinction trying to access AWS credentials, which is accomplished via the @aws-sdk/credential-providers package. In order to get to the actual configuration files, it requires the use of fromIni.

If the build in esbuild platform is set to “browser” (or not specified), then the @aws-sdk package does not export fromIni at all. It’s not available to be used. Changing the esbuild to “node” and building gave it access to that fromIni module and the credential functionality was possible.

This is more of a general note I’m posting just to check if there is any obscure reason why a plugin should not be built as “node”, because it seems to work fine. Also, if anyone wants to correct me or add context. :slight_smile:

And more generally, if this is all fine to do, then this is a note for anyone else attempting to use a package that doesn’t seem to export what is needed based on the build platform (like my case @aws-sdk/credential-provider). If you change the build from browser to node, it may export differently and give you access.

I couldn’t find a specific clear reference to this information when I was looking for it, so I’m posting this here for future readers. Please feel free to comment or indicate why this should or should not be done.

1 Like

On mobile, there is no Node, everything is running in the browser.

Excellent, thanks! That makes total sense. In my case, this is purely a desktop plugin and wouldn’t even make sense to be on mobile due to what it does. So, I hadn’t investigated that aspect.

So, if I summarize:

  • If you’re developing the plugin for desktop, then platform = node or browser is acceptable
  • If you’re developing the plugin for mobile, then platform must be browser.