How to Manage Dependencies in Deno: Simple Guide
In Deno, you manage dependencies by importing modules directly from URLs using
import statements. Deno caches these modules locally and uses a lock.json file to ensure dependency integrity and reproducible builds.Syntax
Deno uses ES module syntax to import dependencies directly from URLs or local files. You write import followed by the module name and the URL or path.
- import: keyword to bring in code
{ something }: named imports from the modulefrom: specifies the source URL or file pathhttps://URL: remote module location
typescript
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; serve(() => new Response("Hello from Deno!"));
Example
This example shows how to import a web server module from Deno's standard library and run a simple HTTP server that responds with a greeting.
typescript
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; serve(() => new Response("Hello from Deno!"));
Output
Listening on http://0.0.0.0:8000/
(When you visit http://localhost:8000 in a browser, it shows: Hello from Deno!)
Common Pitfalls
1. Forgetting to use exact URLs with versions: Always specify the version in URLs to avoid unexpected updates breaking your code.
2. Not using a lock file: Without --lock=lock.json and --lock-write, dependencies can change silently.
3. Importing from untrusted URLs: Only import from trusted sources to avoid security risks.
typescript
/* Wrong: No version specified, risky for breaking changes */ import { serve } from "https://deno.land/std/http/server.ts"; /* Right: Specify exact version for stability */ import { serve } from "https://deno.land/std@0.177.0/http/server.ts";
Quick Reference
| Command | Purpose |
|---|---|
| deno run --lock=lock.json --lock-write script.ts | Run script and create/update lock file for dependencies |
| deno cache script.ts | Download and cache remote dependencies locally |
| import ... from 'https://...' | Import dependencies directly from URLs with version |
| deno info | Show cached dependencies and their locations |
Key Takeaways
Always import dependencies using full URLs with explicit version numbers.
Use a lock file with --lock and --lock-write flags to ensure consistent dependencies.
Deno caches remote modules locally to speed up repeated runs.
Avoid importing from untrusted or unstable URLs to keep your code secure.
Use deno cache and deno info commands to manage and inspect dependencies.