How to dynamically load ESM from CDN or node_modules depending on browser/Node context?

I’m writing an ES6 module that will work either client or server side. For example, it uses mermaid-js, which is both available via CDN URL and installable into Node. To keep the library as small as possible, I’d rather not bundle it with my module. I’d like it to dynamically do the following

  • when it is running in a browser, it loads it mermaid.js from CDN
  • when it is running in Node, it loads it locally, from node_modules.

I do know how to use dynamic imports, and I’m sure I can figure out the code to make it work, but I figured this is one of the hallmark reasons for ESM and there should some standard way to do it, but I found none. I looked far and wide, and all I found was Load file local or from cdn, but that loads the module into a web page, not directly into Javascript or Typescript.