Bundler Usage
Typically srvx
is to be imported like this.
import { serve } from "srvx";
The import above automatically resolves the the correct entrypoint for each runtime. Node.js, Deno, Cloudflare, and Bun use ESM conditions to resolve the correct entrypoint.
Normally, when you are directly using srvx
in your project without bundling it should work as expected.
Using Explicit Imports
Instead of depending on ESM conditions, you can explicitly import srvx
for specific runtime:
import { serve } from "srvx/node";
import { serve } from "srvx/deno";
import { serve } from "srvx/bun";
import { serve } from "srvx/cloudflare";
Using Bundlers
If srvx is being bundled (e.g. by Rollup or esbuild),
the bundler also has to run the ESM resolution algorithm during bundling.
This means the srvx
in the bundle will only work with one specific runtime (usually Node.js).
External Dependency
The simplest way to avoid this is to set srvx
as an external dependency in your bundler.
export default {
//...
external: ["srvx"],
};
import { build } from "esbuild";
await build({
//...
external: ["srvx"], // Add this
});
esbuild main.ts \
# ...
--external:srvx # Add this
By doing this, srvx won't be included in the final bundle, it needs to be available at runtime.
Conditions
Another approach is to set the ESM condition manually at bundle time.
import resolve from "@rollup/plugin-node-resolve";
export default {
//...
plugins: [
resolve({
preferBuiltins: true,
conditions: ["node"], // or "deno", "bun", "workerd", etc.
}),
],
};
import { build } from "esbuild";
await build({
//...
conditions: ["node"], // or "deno", "bun", "workerd", etc.
});
esbuild main.ts \
# ...
--conditions:node # or deno, bun, workerd, etc.
By doing this, the bundler will resolve the correct version on srvx for your runtime.