moodring
moodring5d ago

Local Deno package imports: Use package's deno.json config "exports" map?

I'm working in a Deno workspace where the outer workspace deno.json references a project within (at some sub-dir path). - The project's deno.json uses the "exports" option to map "." to "./src/mod.ts". - The workspace root's deno.json then references this directory in the "imports" map, from "package-a" to "./package-a".
[workspace-root]/
[project-a]/
src/
mod.ts - References ``
deno.json - Maps export "." to "src/mod.ts"
src/
main.ts - References
deno.json - Maps import "project-a" to "./project-a"
[workspace-root]/
[project-a]/
src/
mod.ts - References ``
deno.json - Maps export "." to "src/mod.ts"
src/
main.ts - References
deno.json - Maps import "project-a" to "./project-a"
I would infer/expect (incorrectly?) based on Deno + JSR's import docs, that the outer workspace's import would use the values found in project-a's "exports" map. Running the project returns the following error:
$ deno run -A --unstable-ffi ./src/main.tsx
error: Import 'file:///Users/[user]/[workspace-root]/[project-a]' failed.
0: Is a directory (os error 21)
error: Uncaught (in promise) ProcessError: Command failed.
Exit code: 1
await $`deno run -A --unstable-ffi ./src/main.tsx`;
^
$ deno run -A --unstable-ffi ./src/main.tsx
error: Import 'file:///Users/[user]/[workspace-root]/[project-a]' failed.
0: Is a directory (os error 21)
error: Uncaught (in promise) ProcessError: Command failed.
Exit code: 1
await $`deno run -A --unstable-ffi ./src/main.tsx`;
^
Is my assumption incorrect? Is there something else wrong with my setup? Ultimately, what I'd like is to respect the package's "exports" values, such that 1) I can import with a short specifier, and 2) I can reference either a local directory or the package on JSR without changing any import code.
8 Replies
moodring
moodringOP5d ago
Note: I'm seeing that I might be able to just reference a singular export path and then reference that file directly in the imports, but I'd prefer to keep my existing exports structure (if possible). I also wonder about other unintended side-effects related to bypassing the package level configs.
bartlomieju
bartlomieju4d ago
@dsherret can you advise here?
dsherret
dsherret4d ago
deno.json - Maps import "project-a" to "./project-a"
That will map it to the directory, which isn't supported. If you give project-a a name like @some-scope/project-a in the deno.json then you should be able to just import it with that bare specifier anywhere in the workspace
dsherret
dsherret4d ago
if you ignore the squiggles on project-a, this works
No description
dsherret
dsherret4d ago
i opened https://github.com/denoland/deno/pull/29463 to remove the warning that's shown there
moodring
moodringOP4d ago
Perfect, thank you so much! Seems this works great for projects within the same workspace. An aside: I have a similar use-case, importing packages from a local directory which is adjacent to the root workspace; i.e., two workspaces next to each other, where workspace-a imports workspace-b. As local directory imports aren't supported, I'm just referencing the necessary modules directly in the workspace-a import map. Is this the recommended way to do things?
dsherret
dsherret2d ago
if you have a workspaces outside another workspace you can probably use the "patch" feature (soon to be renamed "link" I think) https://github.com/denoland/deno/issues/25110
GitHub
Tracking: stabilization of "patch" feature · Issue #25110 · denol...
// deno.json { "patch": [ "../some-package-or-workspace" ] } #25068 Questions: Is "patch" an ok name? Isn't it confusing with the idea of "patching a package&...
dsherret
dsherret2d ago
that will link in the specified package or workspace's packages in the current workspace

Did you find this page helpful?