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?
6 Replies
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?
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";
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 careI'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
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 serverDynamic Imports did work, but as I was focusing on simple CLIs, with a Shebang I just wondered if that would cause me issues later. Then, since localhost didn't work for me I rather focused on that, especially since @coty said it should. Now you've reminded me about dynamic imports, I'll have another go... Yes, I'm very impressed with Deno (v2!) and I can feel it's going to be a great alternative to Bash, once I get over the learning curve. I did try Deno years ago but the debugging in vscode didn't work then. Now it's amazing. Thanks.