jradxl
jradxl6d ago

Deno import use for local files?

I'm using Deno (v2) as an alternative to Bash Scripting, and I'm looking for an equivalent to Bash's Source statement, to include any common functions I might write. Of course, import { something } from "./myfunctions,ts" works fine as a relative path, And an absolute path works too, import { something } from "/home/john/.deno/mylibraries/myfunctions.ts". But this hard codes my username in the path. I have found that dynamic imports can be used to avoid this , const myhome = Deno.env.get("HOME") const libpath = myhome + "/.deno/mylibraries/myfunctions.ts"; const myfunctions = await import(libpath); I also thought that a webserver might work, import { something } from "http://localhost/myfunctions.ts"; and it nearly does, shown by the intelisense in vscode, but running gives the error, Expected a JavaScript or TypeScript module, but identified a Unknown module Are their any other/better ways of importing modules? I don't want to make my files public so don't want to use Github Perhaps I could self-host Gitea But all seem overly complex compared to Bash's Source command. Suggestions, please?
5 Replies
coty
coty5d ago
The web server should work, I would use the import map in deno.json. How are you running the server? What’s the MIME type of the typescript file response?
jradxl
jradxl5d ago
I was using PyPi python's httpserver and its documentation doesn't say anything about MIME types. I'll try tomorrow with nginx. I was reading documention on import maps but didn't think it would help (or undertand it) curl -I http://localhost/mod.ts HTTP/1.1 501 Not Implemented Content-Type: text/plain Content-Length: 22 Date: Mon, 30 Sep 2024 23:09:54 +0000 What should it be? I've got a work around by using a non-user related path, and using a unix deno group. import { something } from "/opt/deno/mylibraries/mod.ts";
coty
coty5d ago
For the import map, just makes things cleaner, even in your new example instead of that the import could be just from "library" or whatever you name it I just tried with deno’s file-server from @std/http and it’s all working. And that’s with content type being some weird video format lol, maybe deno doesn’t care
jradxl
jradxl2d ago
I've got it working. I've used Nginx and https, with a real ssl certificate but limited to within my own lan (using pihole as split dns. ie not public). And ensuring the published directory had a deno.json file. I'm not sure what bits caused it to work, but it certainly does now. So my import statement is like: import { wordCount } from "https://myhost.mydomain.dynu.XXX/mydeno/string-utils/mod.ts" Thanks for answering solved
jeff.hykin
jeff.hykin6h ago
I use deno as a replacement to bash (checkout dax, it is fantastic for that) I'm confused, why did dynamic imports not work for you? That seems like way way less work than creating a server