MelodyMaker
MelodyMaker3mo ago

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
2saturdayscode
2saturdayscode3mo ago
HI! You can use an import map specified in your deno.json config file like this:
imports: {
"@root/": "./packages/"
}
imports: {
"@root/": "./packages/"
}
For the latter it's not possible, file extension must be explicit
MelodyMaker
MelodyMakerOP3mo ago
I'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 ../
2saturdayscode
2saturdayscode3mo ago
Can you show me the deno.jsonc?
MelodyMaker
MelodyMakerOP3mo ago
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.
2saturdayscode
2saturdayscode3mo ago
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
{
"unstable": ["unstable-sloppy-imports"]
}
{
"unstable": ["unstable-sloppy-imports"]
}
Read more about it here: https://docs.deno.com/runtime/manual/tools/unstable_flags/#--unstable-sloppy-imports
MOtherMetroid
MOtherMetroid3mo ago
Do 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/
MelodyMaker
MelodyMakerOP3mo ago
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.
MOtherMetroid
MOtherMetroid3mo ago
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.
MelodyMaker
MelodyMakerOP3mo ago
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.
2saturdayscode
2saturdayscode3mo ago
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
MelodyMaker
MelodyMakerOP3mo ago
I'd say it's tied to being a monorepo, which is a large-scale code management and organization design pattern.