如何在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
相关产品推荐
相关产品推荐

