D
Deno

help

I need more help understanding imports and relative paths for a module I am writing

Mmrking2you8/30/2023
I am diving into the world of Deno and I am trying to port an existing node CLI. If you are developing a library, you should instead prefer to use the deps.ts pattern discussed in Managing Dependencies. I have read much of the documentation. but still feel lost on what's required/best practice/quality of life. Maybe I have a lack of understanding elsewhere, but I have a hard time understanding if deps.ts is required for end users when running deno install myModule. The different articles jumping around from using deps.ts, imports in deno.json, to import maps has been overwhelming. I can run the CLI locally on my machine, but when I try to install from deno.land/x, I get many errors like error: Relative import path "enquirer" not prefixed with / or ./ or ../ error: Relative import path "chalk" not prefixed with / or ./ or ../ etc. Is it purely that these npm modules do not work in Deno?
Mmarvinh.8/30/2023
By default ESM modules like in the browser only understands relative imports, not bare specifiers. Bare specifiers for URLs can be mapped via an import map in deno.json and npm modules like in your case can be prefixed with npm: . So instead of doing import * as eqnuirer from "enquirer" do import * as enquirer from "npm:enquirer"
KKyiro8/30/2023
you should probably pin the npm version too* & there's https://deno.land/std@0.200.0/fmt/colors.ts?doc= instead of chalk too if you want to go deeper into the deno ecosystem the deno std provides a lot of goodies but basically, import maps are reserved for programs, and modules on deno.land (or other registries) can't use them unless bundled/transpiled to resolve to full imports or if an import map is passed in as an arg for a program hosted on deno.land so you could do deno run -A --import-map https://deno.land/x/example/importMap.json https://deno.land/x/example/cli.ts iirc same should work with deno install
Mmrking2you8/30/2023
@marvinh. I have enquirer set like this in my import_map.json "enquirer": "npm:enquirer@^2.3.6" So that will work for me, but not for others who run/install the module? i.e. It works when I run it locally, but I start to get the errors if I try to run/install from deno.land.
KKyiro8/30/2023
if you want your module to be available as both a cli and a library, you should use deps.ts or full imports everywhere you can also try this
Mmrking2you8/30/2023
Can I leverage the import map when add deps.ts? edit: maybe I am confused and do not need to set things up as a library. I just want to be able to execute the cli.
KKyiro8/30/2023
what's your module on deno.land/x?
Mmrking2you8/30/2023
https://deno.land/x/octoherdcli My goal is to create a deno version of the existing octoherd/cli. I want to be able to leverage octoherd while writing my scripts in TS. ref: https://github.com/octoherd/cli/pull/135
KKyiro8/30/2023
I got it partly working with this as the import map & deno run -A --import-map ./import_map.json https://deno.land/x/octoherdcli@v0.1.0-alpha/cli.js
{
"imports": {
"@octoherd/octokit": "npm:@octoherd/octokit@^4.0.0",
"octokit/auth-oauth-device": "npm:@octokit/auth-oauth-device@^6.0.0",
"octokit/openapi-types": "npm:@octokit/openapi-types@^8.0.0",
"chalk": "npm:chalk@^5.0.0",
"clipboardy": "npm:clipboardy@^3.0.0",
"enquirer": "npm:enquirer@^2.3.6",
"jsonfile": "npm:jsonfile@^6.0.1",
"mkdirp": "npm:mkdirp@^3.0.0",
"tempy": "npm:tempy@^3.0.0",
"yargs": "npm:yargs@^17.0.0",
"yargs/helpers": "npm:yargs@^17.0.0/helpers",
"octokit/openapi": "npm:octokit/openapi@^12.0.0",
"prettier": "npm:prettier@^3.0.0",
"simple-mock": "npm:simple-mock@^0.8.0",
"uvu": "npm:uvu@^0.5.1"
}
}
{
"imports": {
"@octoherd/octokit": "npm:@octoherd/octokit@^4.0.0",
"octokit/auth-oauth-device": "npm:@octokit/auth-oauth-device@^6.0.0",
"octokit/openapi-types": "npm:@octokit/openapi-types@^8.0.0",
"chalk": "npm:chalk@^5.0.0",
"clipboardy": "npm:clipboardy@^3.0.0",
"enquirer": "npm:enquirer@^2.3.6",
"jsonfile": "npm:jsonfile@^6.0.1",
"mkdirp": "npm:mkdirp@^3.0.0",
"tempy": "npm:tempy@^3.0.0",
"yargs": "npm:yargs@^17.0.0",
"yargs/helpers": "npm:yargs@^17.0.0/helpers",
"octokit/openapi": "npm:octokit/openapi@^12.0.0",
"prettier": "npm:prettier@^3.0.0",
"simple-mock": "npm:simple-mock@^0.8.0",
"uvu": "npm:uvu@^0.5.1"
}
}
change ./import_map.json to the url of your import map from deno.land or github raw @mrking2you
Mmrking2you8/30/2023
I stepped away, but I'm back. Taking a look now. That definitely helped get my past a few errors. They are all similar, and I still don't fully understand the error itself.
โžœ octoherd-cli-deno git:(main) deno run -A --import-map https://raw.githubusercontent.com/kurtaking/octoherd-cli-deno/main/import_map.json https://deno.land/x/octoherdcli@v0.1.2-alpha/index.js
error: Relative import path "octoherd/octokit" not prefixed with / or ./ or ../ and not in import map from "https://deno.land/x/octoherdcli@v0.1.2-alpha/index.js"
at https://deno.land/x/octoherdcli@v0.1.2-alpha/index.js:3:25
โžœ octoherd-cli-deno git:(main) deno run -A --import-map https://raw.githubusercontent.com/kurtaking/octoherd-cli-deno/main/import_map.json https://deno.land/x/octoherdcli@v0.1.2-alpha/index.js
error: Relative import path "octoherd/octokit" not prefixed with / or ./ or ../ and not in import map from "https://deno.land/x/octoherdcli@v0.1.2-alpha/index.js"
at https://deno.land/x/octoherdcli@v0.1.2-alpha/index.js:3:25
Ah, relative import paths in other files are incorrect after the update
KKyiro8/30/2023
yeah i changed the first import map entry to have an @ because smth needed it idk what's up with that
Mmrking2you8/30/2023
No description
Mmrking2you8/30/2023
@Kyiro that did it. wow, thank you for taking the time this (morning?) to help me. I know there are a lot of improvements, opportunities to migrate to deno-specific modules, etc, but it feels SO good to have a working baseline to start from.
KKyiro8/30/2023
you're welcome <:Sadge_pray:862715150742061086>
KKyiro8/30/2023
ty
Mmrking2you9/1/2023
I was making progress and feel like I have hit a wall again. ๐Ÿ˜ข I'm am trying to move away from the hardcoded cli.js file and replace chalk with fmt/colors. I have started to receive relative import paths errors again. This time for the standard fmt module and I followed what was in the docs. deno run -A --import-map https://raw.githubusercontent.com/kurtaking/octoherd-cli-deno/main/import_map.json octoherd.js run -S examples/script.ts error: Relative import path "fmt/colors.ts" not prefixed with / or ./ or ../ and not in import map import map
{
"imports": {
...,
"fmt/": "https://deno.land/std@0.123.0/fmt/"
}
}
{
"imports": {
...,
"fmt/": "https://deno.land/std@0.123.0/fmt/"
}
}

Looking for more? Join the community!

Recommended Posts
Is there an idiomatic/common/modern way of creating a queue of jobs to be executed one after anot...Is there an idiomatic/common/modern way of creating a queue of jobs to be executed one after anotherLaunch tests with unstable flag inside VS CodeHi there, I'm creating a module that uses FFI and Deno.dlopen and I wrote tests to validate some of What is the difference between JS Map and JS Object?I know that `Map` is hash table implementation for JS, but how does it differ from an object? With rDeno KV: Should I expect 'value too large' errors inserting a JSON file with kv.set?I was trying out kv and quickly ran into something I found surprising; ```js let kv = await Deno.oDeno language server stops working in VS CodeAfter some random time the language server stops working and I suddenly get code errors like this: `Getting free disk space without using child_process.Hello everyone! I would like to create a small CLI tool using Typescript and Deno. I would like to mtest --filter a stepCan I only run a certain step in the test?fastwebsockets how to end client calls and avoid Error in websocket connection Unexpected EOFWhat is the issue in this client code that causes the error? ```rust pub async fn connect() -> Resuldeno compile --include argumentIn `deno compile`, how can i include a whole directory instead of just a file? Also, is there a way `deno run` as a library`deno run` does a lot of useful things behind the scenes, many of which are not exposed as a libraryPermissions with npm/node importsI'm trying to run a command and it's like I need to add `--allow-read` permissions because of the `nBroadcastChannelWhen using BroadcastChannel api, in the background does deno send the broadcast to every instance orIs it possible to create a TCP server on Deno Deploy?Is it possible to create a TCP server on Deno Deploy?oak server can't accept requests from another server?hi, I'm trying out deno with oak to make a simple server. I set up cors(x/cors), it accepts requestsaddEventListener for keydown in terminal via deno?Is there something like `addEventlistener("keydown")` in deno, so i can capture terminal inputs?Deno KV Type ErrorWhen I run await (await kv.get<typeThing>(["example"])).id (the value is an object), it returns a tyHow to improve seo ?https://deno.land/x/blog My question is when you use tool like this that work with md, how can i do Hashbang with TS file (without .ts extension)Hi y'all, I have a few convenience scripts written in Typescript for Deno that all have a wrapper scAstro + Deno - Remote ImportsFirst of all thanks a lot for the nice template, it looks great !! I followed the guide posted hereDebugging test in VSCode always breaks on line 54 of colors.tsWhen debugging tests in VSCode the debugger always breaks on line 54 of colors.ts. This behavior is