nitro icon indicating copy to clipboard operation
nitro copied to clipboard

v3: imports starting with '~/'

Open MickL opened this issue 1 month ago • 4 comments

Describe the feature

What I like about Nuxt and Nitro v2 is that I can import from root like this:

import { useLogger } from '~/utils/use-logger';

But in v3 I struggle to do so. I tried to add paths to tsconfig.json but then Vite doesnt find the files. Would be cool if Nitro (or create-nitro-app) auto configures tsconfig/Vite so we can import from root by use of ~

MickL avatar Nov 04 '25 11:11 MickL

If it helps, I have this working using a tsconfig.json file in my root like this:

// SEE https://nitro.unjs.io/guide/typescript
{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "~/*": ["./server/*"]
        },
        "allowImportingTsExtensions": true
    },
    "extends": "./.nitro/types/tsconfig.json",
    "exclude": ["node_modules", "**/node_modules/*", "deno.json"]
}

drewbaker avatar Nov 05 '25 21:11 drewbaker

Are you running a Vite project? Because I think there is no more .nitro folder. With the path option it works for my TypeScript / IDE but Vite doesnt catch up the files.

MickL avatar Nov 05 '25 22:11 MickL

Yeah I think it is Vite, or at least I'm just using the default's of Nitro...

drewbaker avatar Nov 05 '25 23:11 drewbaker

https://github.com/nitrojs/nitro/pull/3757

pi0 avatar Nov 06 '25 23:11 pi0

Example: https://github.com/nitrojs/nitro/tree/main/examples/import-alias

pi0 avatar Nov 09 '25 20:11 pi0

Amazing thanks!! May you release to npm? :)

MickL avatar Nov 09 '25 21:11 MickL

Shortly :)

pi0 avatar Nov 09 '25 22:11 pi0