You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Deno中获取TypeScript文件的本地缓存路径?

获取Deno远程脚本的本地缓存路径

要获取远程脚本在Deno本地缓存中的实际路径,你可以用以下两种方法:

方法一:使用Deno内置API

通过Deno.resolveModuleSpecifier()解析模块URL,直接提取本地缓存路径:

const moduleUrl = import.meta.url;
const resolvedSpecifier = await Deno.resolveModuleSpecifier(moduleUrl);
const localCachePath = new URL(resolvedSpecifier).pathname;

console.log("本地缓存路径:", localCachePath);

执行后会直接返回类似/home/user/.deno/cache/xx/xxx.ts的完整本地路径。

方法二:调用deno info命令

通过执行deno info命令获取模块的详细缓存信息,其中包含本地路径:

const process = await Deno.run({
  cmd: ["deno", "info", import.meta.url],
  stdout: "piped",
});

const output = await process.output();
console.log(new TextDecoder().decode(output));

输出内容里会明确标注该远程脚本在本地缓存中的存储位置。

注意:Deno.resolveModuleSpecifier()需要Deno 1.14及以上版本支持,若使用更早版本,建议用deno info命令的方式。

内容的提问来源于stack exchange,提问作者Owen Young

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 02:15:42