How to resolve an import statement in Deno
Hi, I am testing out Deno, but cannot get it to resolve imports.
For example, how would I get Deno to resolve this import statement...
import { readFileTxt } from '@root/db_fs/src/utilReadFiles';
Notice two things...
1) the "@root" should resolve to "./packages" (i.e. "project-root/packages")
2) if no file extension is specified, a ".js" extension is inferred
Thank you.
11 Replies
HI! You can use an import map specified in your
deno.json
config file like this:
For the latter it's not possible, file extension must be explicitI've tried adding that to both the deno.jsonc and import_map.json and it does not work. I keep getting this error...
error: Relative import path "@root/db_fs/src/utilReadFiles.js" not prefixed with / or ./ or ../
Can you show me the deno.jsonc?
I've tried lots of variations, but essentially just have this in it...
{
"imports": {
"@root/": "./packages/"
}
}
I'm out of ideas. There is just no way to import a file into the Deno runtime. And the error messaging is unhelpful and irrelevant.
Uhmmm this is very strange it should work
try restarting the deno server language
ctrl+shift+p
and write something like "deno restart"
also I've found that maybe you can can omit file extension by enabling an unstable feature in the deno config file like this
Read more about it here: https://docs.deno.com/runtime/manual/tools/unstable_flags/#--unstable-sloppy-importsDo you get the error in the IDE or at runtime? I got that before in the IDE and had to enable the Deno extension:
https://docs.deno.com/runtime/manual/references/vscode_deno/
Yes, I've been doing plenty of restarting extensions, deno servers, and VSC.
I got it working a little but there is a severe limitation. Deno is confused by there being more than one package.json within the project.
Even if the package.json file is empty (or empty curles {}) it fails.
If I delete the package.json or rename it---like for example, "package.jsonDISABLE"---, then it works.
Hm, I don't use Deno with package.json. Can you not import it via
deno add npm:@package
?
Only other thing I remember is that I have to enable the Deno extension by manually enabling it (in my case workspace only). Setting deno.enable
does not work for me for some reason.I can run node funcs fine with the Deno runtime. eg.... import { readFileSync } from 'node:fs'; I'm sure npm works fine, but my problem is more low-level than that. The multiple package.json's have to remain to support multiple subprojects and apps in a repo.
I enable/disable the deno extension to switch between node/deno runtime then switch the VSC debugger launch.json item.
Anyways I give up tinkering with Deno. It's a no-go for me. Maybe I'll looks at v2 when it comes out as it's supposed to have better support for workspaces. But I'm hardpressed to find any practical, real-world, metrics-based advantages to Deno over Node.
I think that it does not make sense the switch for you, if your project is already tied that much to the Node runtime it really does not make sense to switch
I'd say it's tied to being a monorepo, which is a large-scale code management and organization design pattern.