How to Use Import from URL in Deno: Simple Guide
import statement with the full URL string. This allows you to use remote code without installing packages locally, for example: import { serve } from "https://deno.land/std/http/server.ts";.Syntax
Use the import statement followed by the module names in curly braces, then from and the full URL string in quotes. The URL must point to a valid JavaScript or TypeScript module.
- import: keyword to bring in code
- { module }: named exports you want to use
- from: keyword to specify source
- "URL": full web address of the module
import { serve } from "https://deno.land/std/http/server.ts";
Example
This example imports the serve function from Deno's standard library URL and starts a simple HTTP server that responds with "Hello from Deno!".
import { serve } from "https://deno.land/std/http/server.ts"; const server = serve({ port: 8000 }); console.log("Server running on http://localhost:8000/"); for await (const req of server) { req.respond({ body: "Hello from Deno!" }); }
Common Pitfalls
1. Forgetting to use full URL: Deno requires the full URL including protocol (e.g., https://), not just a domain or path.
2. Using unsupported URLs: The URL must point to a valid module file, not a webpage.
3. Not having network permission: Deno runs with secure defaults, so you must allow network access with --allow-net when running scripts that import from URLs.
4. Caching issues: Deno caches remote modules locally; if you want to update, use deno cache --reload.
/* Wrong: Missing protocol */ // import { serve } from "deno.land/std/http/server.ts"; /* Right: Full URL with https */ import { serve } from "https://deno.land/std/http/server.ts";
Quick Reference
| Concept | Description | Example |
|---|---|---|
| Import keyword | Brings in code from a module | import { serve } from "URL"; |
| Full URL | Must include protocol and path | "https://deno.land/std/http/server.ts" |
| Network permission | Allow network access to fetch modules | deno run --allow-net script.ts |
| Cache reload | Force update cached modules | deno cache --reload script.ts |