How to get project directory in nuxt kit ?

I want to create a nuxt module with nuxt kit that add a custom alias. But to do this I need to have the project dir :
nuxt.options.alias = Object.assign(nuxt.options.alias ?? {}, {
mylib: resolve(projectDir, './node_modules/mylib')
})
nuxt.options.alias = Object.assign(nuxt.options.alias ?? {}, {
mylib: resolve(projectDir, './node_modules/mylib')
})
How can I acheve such a thing ?
8 Replies
harlan
harlan2y ago
use the createResolver function
const { resolvePath } = createResolver(import.meta.url)
// this might work
resolvePath('mylib')
const { resolvePath } = createResolver(import.meta.url)
// this might work
resolvePath('mylib')
you should be able to import like that, if not you have access to nuxt.options.rootDir
Yamakasinge
Yamakasinge2y ago
This seems to work pretty well with resolvePath('mylib'). However I am using sass and it cannot find import : @import '~mylib/mystyle.scss' I am doing thing right, or for sass i need something else ?
harlan
harlan2y ago
hm ye this requires something else maybe, let me think does it show the alias resolving at all?
Yamakasinge
Yamakasinge2y ago
In the tsconfig.json file generated by nuxt it seems good. But sass doesn't seem to resolve at all
harlan
harlan2y ago
hm it says they should be respected
Vite aliases are also respected for CSS @import
is this in dev and built?
Yamakasinge
Yamakasinge2y ago
If I try to import the scss file in a ts file, it doesn't resolve import as well. I think there is something wrong with my configuration.
harlan
harlan2y ago
that doesn't seem like something nuxt would support what are you trying to do exactly
Yamakasinge
Yamakasinge2y ago
Okay I think I found the issue. I am trying to create a nuxt module 'mylib-nuxt'. I want 'mylib-nuxt' to expose 'mylib-styles' as an alias to 'mylib/styles/*.scss'. The issue comes from the resolvePath('mylib/styles') which resolve to mylib/dist/node_modules/mylib. I want the dist part away. Maybe I should just use resolvePath('../mylib/styles') ? Okay this was the solution : On the nuxt module : nuxt.options.alias['mylib-styles'] = await resolvePath('~~/node_modules/mylib/styles) And in the scss file : @import "mylib-styles/mixins.scss";
Want results from more Discord servers?
Add your server