18

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?

Evandro Pomatti
  • 13,341
  • 16
  • 97
  • 165
xcatliu
  • 791
  • 7
  • 19

4 Answers4

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

Evandro Pomatti
  • 13,341
  • 16
  • 97
  • 165
8

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.

Alex Sedeke
  • 208
  • 3
  • 8
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';

xcatliu
  • 791
  • 7
  • 19
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; 
Ahmed Hammad
  • 2,798
  • 4
  • 18
  • 35