xcatliu
xcatliu

Reputation: 839

How to delete runtime import cache in deno?

In node.js we can use

delete require.cache[require.resolve(somePath)];

to delete the require cache in runtime.

Is there a similar way to delete runtime import cache in deno?

Upvotes: 22

Views: 13075

Answers (5)

Dan Dascalescu
Dan Dascalescu

Reputation: 151702

This feature was added in Deno v1.46 (August 2024):

deno clean

Deno has a global cache that you can control with the DENO_DIR environmental variable. This cache is used to store remote dependencies for efficient use across multiple projects, V8 code cache for faster startups, as well as caches for subcommands such as deno fmt, deno lint and others to minimize work done on each run.

You can now quickly purge the whole cache by invoking deno clean

Upvotes: 1

Alex Sedeke
Alex Sedeke

Reputation: 237

So far I have not found a command that clears the cache. But it is possible to get the current cache directories from deno using deno info. The output will look like this:

DENO_DIR location: "/Users/tgm/Library/Caches/deno"
Remote modules cache: "/Users/tgm/Library/Caches/deno/deps"
Emitted modules cache: "/Users/tgm/Library/Caches/deno/gen"
Language server registries cache: "/Users/tgm/Library/Caches/deno/registries"

So deleting the files inside DENO_DIR directory, clear the cache. Hope this is helpful.

Upvotes: 9

xcatliu
xcatliu

Reputation: 839

Add a random querystring to the path, be sure to keep the correct extname:

const ext = path.extname(somePath);
const mod = (await import(`${somePath}?version=${Math.random()}${ext}`)).default;

It also support local file path like const somePath = '../foo.tsx';

Upvotes: 3

Ahmed Hammad
Ahmed Hammad

Reputation: 3095

Deno supports dynamic imports since August '19, what I think you can do is something like

let neededModule = getNeededModule();
import neededModule;
...
neededModule  = getAnotherModule(); //Replace in runtime
import neededModule
...
//Or even delete in runtime (with some help from the garbage collector)
neededModule = null; 

Upvotes: 1

Evandro Pomatti
Evandro Pomatti

Reputation: 15144

The -r or --reload option will recompile the imported modules.

-r, --reload=<CACHE_BLACKLIST>     Reload source code cache (recompile TypeScript)

https://deno.land/manual#other-key-behaviors

Other key behaviors

  • Remote code is fetched and cached on first execution, and never updated until the code is run with the --reload flag. (So, this will still work on an airplane.)
  • Modules/files loaded from remote URLs are intended to be immutable and cacheable.

You can pass arguments to reload specific modules:

--reload=https://deno.land/std

https://deno.land/manual/linking_to_external_code/reloading_modules

Upvotes: 17

Related Questions