N
Nuxt3w ago
Timo

Nuxt layers relative path

I have 2 nuxt projects, project 1 and project 2. Project 1 uses the
extends:['../project2'],
extends:['../project2'],
to utilize this project as a layer. Inside the server folder of project2 I have a folder for database models that get used in the api. This all works perfectly fine when i'm running project2 as a dev server, but when starting up project1 it can't load these models.
[nitro 20:37:32] ERROR Error: Could not load project1/server/models/translation (imported by ../project2/server/api/translations/index.get.ts): ENOENT: no such file or directory, open 'project1\server\models\translation'
[nitro 20:37:32] ERROR Error: Could not load project1/server/models/translation (imported by ../project2/server/api/translations/index.get.ts): ENOENT: no such file or directory, open 'project1\server\models\translation'
Looking at the error it makes sense because my project1 does not have a models folder inside the server folder and with the import
import TranslationModel from '~/server/models/translation';
import TranslationModel from '~/server/models/translation';
using a relative path it makes sense. Is there any way I can make it so this import refers to my project2 folder instead of project1?
10 Replies
kapa.ai
kapa.ai3w ago
Hi @Timo. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Your question is about using Nuxt layers and relative paths in a multi-project setup, which is a relevant topic for Nuxt. I'll address your issue based on the provided knowledge sources. The problem you're experiencing is related to how Nuxt resolves paths when using layers. When you use relative paths or aliases like ~/ in a layer, they are resolved relative to the user's project (in this case, project1) instead of the layer's project (project2). This behavior is documented in the Nuxt Layers documentation. To solve this issue, you have a few options: 1. Use full resolved paths: Instead of using relative paths or aliases in your layer (project2), you can use full resolved paths. Here's an example of how you can do this:
import { fileURLToPath } from 'url'
import { dirname, join } from 'path'

const currentDir = dirname(fileURLToPath(import.meta.url))

// Use this to import your model
import TranslationModel from join(currentDir, '../models/translation')

import { fileURLToPath } from 'url'
import { dirname, join } from 'path'

const currentDir = dirname(fileURLToPath(import.meta.url))

// Use this to import your model
import TranslationModel from join(currentDir, '../models/translation')

This approach ensures that the path is resolved relative to the layer's directory. 2. Use the #imports alias: As mentioned in a GitHub issue comment, you can use the #imports alias and make sure your models directory is added to the list of auto-importable directories. This method allows for easier overriding in the main project.
import { TranslationModel } from '#imports'

import { TranslationModel } from '#imports'

To make this work, you'll need to add the models directory to your auto-imports configuration in your nuxt.config.ts:
export default defineNuxtConfig({
imports: {
dirs: ['server/models']
}
})

export default defineNuxtConfig({
imports: {
dirs: ['server/models']
}
})

3. Use the experimental localLayerAliases feature: Nuxt has an experimental feature called localLayerAliases that can help resolve aliases within layers. You can enable it in your nuxt.config.ts:
export default defineNuxtConfig({
experimental: {
localLayerAliases: true
}
})

export default defineNuxtConfig({
experimental: {
localLayerAliases: true
}
})

This feature is enabled by default in recent Nuxt versions, so you might not need to explicitly enable it if you're using a recent version. By implementing one of these solutions, you should be able to properly import your models from project2 when running project1. Remember to run nuxi prepare after making changes to your configuration to ensure everything is properly set up. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@292000534162964481> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Timo
TimoOP3w ago
@kapa.ai all 3 methodes don't seem to work..
kapa.ai
kapa.ai3w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I apologize that the suggested methods didn't resolve your issue. Since you've tried the recommended approaches without success, it's important to investigate further. However, based on the information provided in the knowledge sources, I don't have enough specific details to offer a definitive solution to your particular case. Given that the standard solutions aren't working as expected, there might be some unique aspects of your project setup or configuration that are causing this persistent issue. At this point, it would be beneficial to: 1. Double-check your Nuxt version to ensure you're using the latest stable release, as some fixes and improvements related to layers and path resolution have been made in recent versions. 2. Verify that your layer (project2) is correctly set up and that the extends configuration in project1 is pointing to the right location. 3. Consider creating a minimal reproduction of your issue, as suggested in the Nuxt bug reporting guidelines. This can help isolate the problem and make it easier to identify the root cause. If you're still facing difficulties after these steps, it would be advisable to seek help through the official Nuxt channels. You can: - Ask for help in the Nuxt Discord community - Open a discussion on the Nuxt GitHub repository These platforms can provide more interactive assistance and might help identify any project-specific issues that are not covered in the general documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@292000534162964481> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Kal
Kal3w ago
What if you were to make an empty models folder in project1, or further, a basic file with the top-level wrapping, but nothing inside. Side note: this AI bot help seems to be really heavy-handed, yeesh.
Timo
TimoOP2w ago
@Kal thanks for the reply. Making a folder with empty file works ofcource but i need the model itself.. an empty file won't work. I actually accidentally figured it out but still have some questions.. Importing it like so: import TranslationModel from '~/server/models/translation'; won't work. But an import like so: import TranslationModel from '../../models/translation'; will work. I'm not exactly sure what the difference is however.. except the ~or @ indicates a relative path
dwol
dwol2w ago
have you tried creating an alias? https://nuxt.com/docs/api/nuxt-config#alias I have this working for my current project
Nuxt
Nuxt Configuration
Discover all the options you can use in your nuxt.config.ts file.
dwol
dwol2w ago
In project 1:
alias: {
'#baseWeb': fileURLToPath(new URL('./app', import.meta.url))
},
alias: {
'#baseWeb': fileURLToPath(new URL('./app', import.meta.url))
},
Then in project 2:
import something from '#baseWeb'
import something from '#baseWeb'
Kal
Kal2w ago
Oh yes that is the case, on server side you need to specify explicit paths.

Did you find this page helpful?