How to delete runtime import cache in deno?
Asked Answered
B

4

19

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?

Burglary answered 20/5, 2020 at 2:21 Comment(0)
D
17

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

Demivolt answered 20/5, 2020 at 2:34 Comment(1)
what is runtime cachePapoose
I
9

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.

Interfile answered 18/4, 2021 at 8:47 Comment(0)
B
4

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';

Burglary answered 20/5, 2020 at 2:55 Comment(0)
N
2

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; 
Niobe answered 20/5, 2020 at 2:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.