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 Types
Section titled “Import Types”NPM Packages
Section titled “NPM Packages”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 });}JSR Packages
Section titled “JSR Packages”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);}URL Imports
Section titled “URL Imports”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 });}Relative Imports
Section titled “Relative Imports”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 });}Important: Full Specifiers Required
Section titled “Important: Full Specifiers Required”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:
// Correctimport { z } from "jsr:@zod/zod";import dayjs from "npm:dayjs";
// Will not workimport { z } from "zod";import dayjs from "dayjs";Complete Example
Section titled “Complete Example”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(), });}What’s Next
Section titled “What’s Next”- Learn about the Handler Context available to your functions
- Create Your First Function if you haven’t already