N
Nuxt9mo ago
Flo

Nuxt Layer: Composable from layer not found

Following directory structure:
/workspaces
/project
/app (the main nuxt app)
/server
/api
/someHandler.get.ts (I want to use useK8s here)
/auth (a layer)
/k8s (another layer)
/composables
/k8s.ts (contains an exported function called useK8s)
/workspaces
/project
/app (the main nuxt app)
/server
/api
/someHandler.get.ts (I want to use useK8s here)
/auth (a layer)
/k8s (another layer)
/composables
/k8s.ts (contains an exported function called useK8s)
For unknown reason, I can't use useK8s in someHandler.get.ts. Nuxt complains: ERROR [nuxt] [request error] [unhandled] [500] useK8s is not defined . Yet my app/.nuxt/imports.d.ts contains export { useK8s } from '../../k8s/composables/k8s';
6 Replies
Flo
FloOP9mo ago
(The composable even shows up in the nuxt devtools)
Alexzvn™
Alexzvn™9mo ago
have you tries import { useK8s } from '~/../k8s/composables/k8s' in someHandler.get.ts
Flo
FloOP9mo ago
yeah, that works. But auto-imports should also work. I just tried moving my composable to a server util - and I can suddenly use it
Nasr galal
Nasr galal8mo ago
sometimes server/utils can be a good workaround. Or server/plugins where you could inject some stuff in nitro hooks
Flo
FloOP8mo ago
Yup. Had to remember that composables are not usable in api handlers. Confusing when there's a lot of stuff looking like composables in nitro and nuxt 🙈
davestewart
davestewart8mo ago
AFAIK autoimports are for core folders only; you'll have to set up layer imports manually. I have used the layer config option components for auto-importing layer components, but I think maybe the imports statement is what you want here. https://nuxt.com/docs/api/nuxt-config#imports I don't know if it's supposed to be absolute paths or folders... the docs never seem to be clear about this. In components, you can use aliases, so might be something like:
// in the layer config
imports: [
'~~/k8s/composables'
]
// in the layer config
imports: [
'~~/k8s/composables'
]
Want results from more Discord servers?
Add your server