0
0
DenoHow-ToBeginner ยท 3 min read

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 module
  • from: specifies the source URL or file path
  • https:// 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

CommandPurpose
deno run --lock=lock.json --lock-write script.tsRun script and create/update lock file for dependencies
deno cache script.tsDownload and cache remote dependencies locally
import ... from 'https://...'Import dependencies directly from URLs with version
deno infoShow 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.