Here is a guideline about how to achieve this goal. I’ll list the most general principles first then explain them in detail and finally give you a recommended pattern to develop this kind of library.
To make the discussion easier, we will call the ability to run on any ECMAScript environment as having zero host dependency.
When we say run on any ECMAScript environment, we mean: users of the library can run this library on a modern ECMAScript environment (at least ES2015) that supports the ECMAScript module with proper setups (e.g. provide IO functions) without modifying the library code.
Do not use anything that is not defined in the ECMAScript specification directly. (e.g. Web or Node APIs).
Do not use module specifier (
import x from "./file.js") to refer to any module.
Be aware of language features (
Math.random(), etc…) are disabled in some special environments.
If you’re strictly following the rules described above, you’ll find that you’re almost not able to develop anything. But this article is going to introduce a comfortable paradigm for developing libraries with zero host dependency.