vrugtehagel
vrugtehagel2d ago

Read file as text in JSR package (easy with NPM, not Deno)

I'm writing a JSR package. My problem boils down to; I want to read a file (that is part of the package) as text, within a script (also part of the same package). I can get this to work fine if I use NPM to install this JSR package, but in Deno it does not work; the import.meta.url inside the script that needs to read the text file resolves to a https://jsr.io/… URL, whereas the text file is presumably ready to be read on the user's machine given the package has been installed. How do I read the text file? Since it is a JSR package, I cannot rely on the CWD.
5 Replies
Mrcool 🇵🇸
It seems to work , u can test this package import * as jsr_test from "@sigmasd/jsr-test"; I read the file like this
console.log(await fetch(import.meta.resolve("./b.txt")).then((r) => r.text()));
console.log(await fetch(import.meta.resolve("./b.txt")).then((r) => r.text()));
Also the url of the file ends being a local one , not a remote https url
vrugtehagel
vrugtehagelOP2d ago
Yeah, this works, but it doesn't resolve to a local URL for me - it resolves to a JSR URL, and it requires network permissions to run Actually, correction; it does work correctly and resolves to a local path if it is a project using a package.json, but not when it uses a deno.json Created denoland/deno#28217
Mark G
Mark G22h ago
I created a module for this very thing (until with { type: "text" } is supported)... https://jsr.io/@jollytoad/import-content
JSR
@jollytoad/import-content - JSR
@jollytoad/import-content on JSR: Import arbitrary file content in Deno using module resolution algorithms.
vrugtehagel
vrugtehagelOP18h ago
Thanks - the GitHub issue's been closed and I guess I'm stuck waiting for with { type: "text" }. Does that package of yours still require net permissions? Right now I've gone for the hacky export const foo = 'content…' route, inlining the file and pretending it's JS so I can import it. Not pretty, but does the job without unnecessary permissions
Mark G
Mark G17h ago
it uses the deno cache, so it'll only need net access if it's not already cached, i guess it may also not need net if you enable vendoring (i've not tested that though)

Did you find this page helpful?