I need more help understanding imports and relative paths for a module I am writing
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.17 Replies
Is it purely that these npm modules do not work in Deno?
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"
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
@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.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 thisCan 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.what's your module on deno.land/x?
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/135I 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
change ./import_map.json
to the url of your import map from deno.land or github raw
@mrking2youI 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.
Ah, relative import paths in other files are incorrect after the update
yeah i changed the first import map entry to have an
@
because smth needed it
idk what's up with that@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.
you're welcome <:Sadge_pray:862715150742061086>
small shoutout š
https://github.com/kurtaking/octoherd-cli-deno/releases/tag/v0.1.3-alpha
ty
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