4 ms·
Does serverside JS have anything similar to #ifdef?
by bryancoxwell 3y ago
Does serverside JS have anything similar to #ifdef?
- antipurist 3y agoSomething like if ('Bun' in globalThis) ... if ('Deno' in globalThis) ... if ('window' in globalThis) ... could be used to determine the runtime.
- timw4mail 3y agoThat certainly works for Bun and Deno, not sure about what is effective in Node, though.
- basil-rash 3y agoChecking for process.versions.{node,bun} works, not sure if Deno has an equivalent.
- deleted 3y ago[deleted]
- vlovich123 3y agoYes. You can define a constant (eg IS_SERVER) which your bundler (eg esbuild) replaces with a specific value depending on what environment you’re targeting. There are also ways to detect it using runtime globals that are only defined within the browser although that can be monkeypatched by your own code or 3p libraries you import (not common but could be a compat layer you add to make server side JS behave more like a browser).