Caching your deno deps in a specified directory

Assume you have a webserver.js script with the following contents

import { serve } from "[email protected]/http/server.ts"

const port = 38080

// ...the rest of the code

You’re coming from the NodeJS world and you’re used to node_modules/ in the project directory. Deno caches dependencies, but not in the project directory. They’re in a location like ~/.cache/deno.

You think is a good thing to have your dependencies cached in a directory in the project, so let’s get that working.

You do it with the DENO_DIR env var. So to have all the deps for our script (above) stored in a directory in the project, we can do

DENO_DIR=$PWD/deps-cache deno cache webserver.js

Note that the directory doesn’t need to already exist, it’ll be created. That previous command will produce a dir structure something like (truncated to be easier to read)

$ tree deps-cache
├── deps
│   └── https
│       └──
│           ├── 0c58cb73b5d6da4c2fedcaa1824342eb8f99846672923517cafbed8589584a49
│           ├── 0c58cb73b5d6da4c2fedcaa1824342eb8f99846672923517cafbed8589584a49.metadata.json
│           ...
└── gen
    ├── file
    │   └── tmp
    │       └── tmp.SgmiznSvYm
    │           └── webserver.js.buildinfo
    └── https
            ├── 0c58cb73b5d6da4c2fedcaa1824342eb8f99846672923517cafbed8589584a49.js
            ├── 0c58cb73b5d6da4c2fedcaa1824342eb8f99846672923517cafbed8589584a49.meta

And, we can run our script using this same cache by using the same env var to the run command:

DENO_DIR=$PWD/deps-cache deno run webserver.js

You might also want to checkout

comments powered by Disqus