Skip to content

Third-Party Packages

Function handlers in Crude Functions run on Deno, which uses URL-based imports. This page covers how to import external packages from NPM, JSR, and other sources.

Import packages from NPM using the npm: prefix:

import { camelCase } from "npm:lodash-es";
import dayjs from "npm:dayjs";
export default async function (c, ctx) {
const formatted = camelCase("hello world");
const date = dayjs().format("YYYY-MM-DD");
return c.json({ formatted, date });
}

Import packages from JSR (JavaScript Registry) using the jsr: prefix:

import { z } from "jsr:@zod/zod";
const UserSchema = z.object({
email: z.string().email(),
name: z.string().min(1),
});
export default async function (c, ctx) {
const body = await c.req.json();
const result = UserSchema.safeParse(body);
if (!result.success) {
return c.json({ error: result.error.issues }, 400);
}
return c.json({ user: result.data }, 201);
}

Import directly from URLs:

import confetti from "https://esm.sh/canvas-confetti";
export default async function (c, ctx) {
// Use the imported module
return c.json({ loaded: true });
}

Import other files from your code/ directory using relative paths:

import { formatGreeting } from "./utils/formatting.ts";
import { validateInput } from "./validators.ts";
export default async function (c, ctx) {
const greeting = formatGreeting("World");
return c.json({ message: greeting });
}

You must use the full specifier prefix (npm:, jsr:, or a complete URL) for external packages. Short aliases like "lodash" or "zod" will not work:

// Correct
import { z } from "jsr:@zod/zod";
import dayjs from "npm:dayjs";
// Will not work
import { z } from "zod";
import dayjs from "dayjs";

Here’s a handler that uses multiple import types:

import { z } from "jsr:@zod/zod";
import dayjs from "npm:dayjs";
import { getUser } from "./utils/db.ts";
const QuerySchema = z.object({
id: z.string().uuid(),
});
export default async function (c, ctx) {
const result = QuerySchema.safeParse(ctx.query);
if (!result.success) {
return c.json({ error: "Invalid query parameters" }, 400);
}
const user = await getUser(result.data.id);
return c.json({
user,
fetchedAt: dayjs().toISOString(),
});
}