mrking2you
mrking2youā€¢16mo ago

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
mrking2you
mrking2youOPā€¢16mo ago
Is it purely that these npm modules do not work in Deno?
marvinh.
marvinh.ā€¢16mo ago
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"
Kyiro
Kyiroā€¢16mo ago
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
mrking2you
mrking2youOPā€¢16mo ago
@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.
Kyiro
Kyiroā€¢16mo ago
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
mrking2you
mrking2youOPā€¢16mo ago
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.
Kyiro
Kyiroā€¢16mo ago
what's your module on deno.land/x?
mrking2you
mrking2youOPā€¢16mo ago
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
Kyiro
Kyiroā€¢16mo ago
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
mrking2you
mrking2youOPā€¢16mo ago
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
Kyiro
Kyiroā€¢16mo ago
yeah i changed the first import map entry to have an @ because smth needed it idk what's up with that
mrking2you
mrking2youOPā€¢16mo ago
No description
mrking2you
mrking2youOPā€¢16mo ago
@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.
Kyiro
Kyiroā€¢16mo ago
you're welcome <:Sadge_pray:862715150742061086>
Kyiro
Kyiroā€¢16mo ago
ty
mrking2you
mrking2youOPā€¢16mo ago
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/"
}
}