import defineNitroPlugin or not? Import aliases not working in /server/plugins/*.ts
Hi, I created a new Project today on the newest version 4.2.1 and for some reason I dont get type hints for defineNitroPlugin anymore. Also, I cannot import from e.g. "~~/server/utils/auth".
This works in server utils and API routes, but for some reason, in plugins, it does not.
If I run the dev server everything works. I am using this plugin to create a better-auth superuser (I just need 1 user), is there another way I can / should do this?
In the docs it tells me: "It is necessary to explicitly import defineNitroPlugin from nitropack/runtime within your plugin file.", but I have another app in which the type completion works, also on the newest nuxt version.
I tried:
- deleting node_modules and .nuxt folders and recreating them multiple times.
- restarting dev server
If I need to explicitly import it, that is fine by me, but why does my other app work? đ
12 Replies
Hi @Dustin. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Youâve run into two separate but related things:
1.
defineNitroPlugin import / typings
In custom Nuxt modules, Nitro plugins do not work âmagicallyâ unless you explicitly import Nitro/runtime helpers like defineNitroPlugin and useRuntimeConfig in the plugin file itself. This was important enough that it was added as a docs warning: you must import them directly rather than relying on autoâimports. [custom module warning]
Thatâs why youâll see examples that do:
If this file is part of a module, trying to use #build/types/nitro-imports or other app aliases inside the server runtime can trigger âVue app aliases are not allowed in server runtimeâ and missingâfile errors, so you should not rely on that pattern. [StackOverflow nitro plugin]
The change in Nuxt 4.1â4.2 around separate TS projects and tsconfig generation can affect how/where those types are included, so one project may still have working editor inference from older behavior while a fresh project follows the newer, stricter setup. There isnât enough detail in the sources to say exactly why your other app still resolves defineNitroPlugin without explicit import, but current guidance is to import it explicitly.
2. Aliases / autoâimports in server context
- Nuxt 4 introduced a dedicated shared/ directory (with #shared alias) whose types are wired into both tsconfig.json and tsconfig.server.json, and whose utils/types are autoâimportable from both app and server. [shared folder; shared types server]
- There have been fixes to make sure serverâside tsconfig includes server files in layers and shared types, mainly to fix editor errors like âCannot find name defineNitroPluginâ even though the app runs. [layer server tsconfig; shared types server]
- Another fix restored Nuxt aliases into Nitroâs compilerOptions.paths so things like #imports/#components and other aliases work correctly in server TS context. [restore nitro aliases]
The issue you describe â aliases like ~~/server/utils/auth working in server routes but not in server/plugins/*.ts, plus missing type hints â matches these TSâconfig / include issues. In 4.2.1, the patches above should be in place, but IDEs (especially JetBrains) have had bugs with Nuxt typings and may not fully respect the generated multiâtsconfig setup. [JetBrains TS bug note]
What you should do now
- In server plugins, explicitly import Nitro helpers:
- Use standard Nuxt aliases (#imports, #shared, etc.) where possible; if ~~/server/... is not being resolved in the plugin but works elsewhere, thatâs likely your IDE reading the wrong tsconfig or not picking up the generated .nuxt/tsconfig.server.json. The runtime behavior being correct but types failing is exactly what the tsconfig fixes were addressing. [layer server tsconfig]
- For creating a superuser in Nitro, your current approach (Nitro plugin that runs on server startup / on first request and uses useRuntimeConfig) is consistent with the documented pattern; the sources donât describe an alternative ârecommendedâ superuser mechanism beyond standard Nitro plugins, so thereâs no clearly better documented pattern to point to.
Because the knowledge sources donât include your exact repo setup, I canât say precisely why the other app still has autoâcompletion without explicit imports, but the current documented and supported pattern is to always import defineNitroPlugin (and similar) explicitly in server plugins, especially when theyâre registered from a custom Nuxt module.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@239748199818985472> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai what should my root tsconfig.json look like?
Let me check the knowledge base for you again. One moment! :books:
----------------------
For Nuxt 4 with the new multiâproject setup, the docs and issues consistently show the root
tsconfig.json as a project reference file that mainly points to the generated configs and (optionally) adds shared compilerOptions.
The minimal recommended shape is:
[typecheck issue; shared tsconfig issue]
If you need global compiler options (e.g. decorators), you can add them at the root level; these are then picked up by the referenced projects:
[shared tsconfig issue]
The sources donât provide a more opinionated full template (e.g. for paths/aliases) beyond this pattern.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@239748199818985472> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai found the "error", mine was like this:
{
// https://nuxt.com/docs/guide/concepts/typescript
"extends": "./.nuxt/tsconfig.json"
}
Let me check the knowledge base for you again. One moment! :books:
----------------------
That
extends setup is exactly what causes problems with Nuxt 4âs new TypeScript project references.
Nuxtâs docs and migration notes explicitly say you must remove:
when you switch to the new referencesâbased config, because extends and references are mutually exclusive and lead to lots of unexpected TS errors if combined. [extends removal]
So your fix is correct: replace the old config with the projectâreferences style one, e.g.:
[typecheck issue]
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@239748199818985472> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Let me check the knowledge base for you again. One moment! :books:
staubinger has been warned
Reason: Mass mention
----------------------
Nuxtâs own docs and issues only give partial guidance here, not a full âofficial minimal
However, the entries like
package.jsonâ, so some of this canât be answered with absolute authority. What is clear from the sources:
1. Is nuxt a dependency or devDependency?
In all the Nuxt 3/4 projects and examples in the sources, nuxt is always in devDependencies, not dependencies. Examples:
[Rollup error example]
[nuxt image prod]
So, based on the examples Nuxt provides, keep nuxt in devDependencies.
2. Are Nuxt modules dependencies or devDependencies?
Across the examples, Nuxt modules are almost always listed in devDependencies (when used in a standard Nuxt app, not as libraries):
- @nuxt/image in devDependencies [nuxt image prod]
- @nuxtjs/tailwindcss in devDependencies [Rollup error example]
- @nuxt/devtools in devDependencies [nuxt image prod; DevTools getting started]
There is one exception in the GAE answer where everything (including nuxt and modules) was moved to dependencies just to work around that platformâs install behavior, but the author explicitly notes it might not be necessary and did it as a workaround. [GAE deployment]
So: for a normal Nuxt app, put Nuxt modules (like @nuxtjs/tailwindcss, @nuxt/devtools, etc.) in devDependencies.
3. âMinimalâ Nuxt project dependencies
The sources donât define an official âminimalâ set, but typical small apps in the docs look like:
with extra modules like @nuxt/image, @nuxtjs/tailwindcss, etc. also in devDependencies. [nuxt image prod; DevTools getting started]
Your own app-specific runtime libraries (e.g. better-auth, better-sqlite3, AWS SDK clients) should be in dependencies, because theyâre used by the built server code at runtime.
4. Feedback on your snippet
Based on how Nuxt projects in the sources are structured:
is consistent with the patterns shown in the sources.However, the entries like
"/devtools", "js/tailwindcss", "/icon", "/kit" donât appear anywhere in the Nuxt docs or examples provided, so I canât confirm what they are or how they should be classified. If theyâre meant to be @nuxt/devtools, @nuxtjs/tailwindcss, nuxt-icon, @nuxt/kit, then the examples show those being used as devDependencies in regular apps. [nuxt image prod; DevTools getting started]
Because the knowledge sources donât contain a single canonical âthis is the minimal package.json for Nuxtâ, I canât give a stricter answer than this patternâbased guidance.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@239748199818985472> Kapa.ai is still learning and improving, please let me know how I did by reacting below.