0
0
DenoHow-ToBeginner ยท 3 min read

How to Use Deno Cache Command for Efficient Module Management

Use the deno cache command to pre-download and store remote modules locally without running the script. This helps speed up execution and allows offline use by caching dependencies ahead of time.
๐Ÿ“

Syntax

The deno cache command syntax is simple. You provide one or more script files or URLs to cache their dependencies.

  • deno cache [options] ...

Options include flags like --reload to force re-downloading modules.

bash
deno cache [options] <file_or_url> ...
๐Ÿ’ป

Example

This example caches the dependencies of a remote Deno script without running it. It downloads and stores all imported modules locally.

bash
deno cache https://deno.land/std@0.177.0/examples/welcome.ts
Output
Check https://deno.land/std@0.177.0/examples/welcome.ts Download https://deno.land/std@0.177.0/io/bufio.ts Download https://deno.land/std@0.177.0/fmt/colors.ts ... (other dependencies cached)
โš ๏ธ

Common Pitfalls

One common mistake is expecting deno cache to run the script; it only downloads dependencies. Another is forgetting to use --reload when you want to update cached modules, which can cause stale code to run.

Also, caching local files without remote imports has no effect since local files are always available.

bash
deno cache script.ts  # Correct: caches dependencies

deno run script.ts      # Runs the script

deno cache --reload script.ts  # Forces fresh download of dependencies
๐Ÿ“Š

Quick Reference

CommandDescription
deno cache Download and cache dependencies without running
deno cache --reload Force re-download of all dependencies
deno cache --unstable Use unstable APIs while caching
deno cache --quiet Suppress output messages
โœ…

Key Takeaways

Use deno cache to prefetch and store remote modules locally before running scripts.
Add --reload to refresh cached modules when dependencies change.
deno cache does not execute code; it only downloads dependencies.
Caching improves performance and enables offline usage of Deno scripts.
Local files without remote imports do not benefit from caching.