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?
This is because for this run, instead of compiling again, Deno is directly using the cached version in gen/. The cache load and save code could be found in DenoDir::load_cache and DenoDir::code_cache, from src/deno_dir.rs.
The cache load and save code could be found in DenoDir::load_cache and DenoDir::code_cache, from src/deno_dir.rs. To force Deno to recompile your TypeScript code instead of using the cached version, you could use the --recompile flag. $DENO_DIR/deps is used to store files fetched through remote url import.
Also note a JavaScript module is imported directly into a TypeScript module, Deno has no problem handling this. Command: deno run ./remote.ts In the local import example above the add and multiply functions are imported from a locally stored arithmetic module.
The compilation message is gone. This is because for this run, instead of compiling again, Deno is directly using the cached version in gen/. The cache load and save code could be found in DenoDir::load_cache and DenoDir::code_cache, from src/deno_dir.rs.
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';
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.
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
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With