How to get root path of a nuxt project?
I am trying to make nuxt create new directories for me during build step. How can I do it?
I am using
mkdirSync(join(process.cwd(), 'new-dir'))
on nuxt's ready
build hook but it ofc creates the directory on the path where I run the build command because of process.cwd(). How can I get build's root directory?10 Replies
Hi @zigglearner1235. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To get the root directory of a Nuxt project during the build step, you should use the Nuxt instance's options rather than relying on
process.cwd()
. Specifically, you can access nuxt.options.rootDir
inside Nuxt hooks, which gives you the actual root directory of the project, regardless of where the build command is run.
Here's how you can use it in your nuxt.config
with the build:done
hook (or any other build-time hook):
This ensures the directory is created in the Nuxt project's root, not the current working directory from which the command is run. The documentation example for hooks demonstrates this pattern, using builder.nuxt.options.buildDir
—you can similarly use builder.nuxt.options.rootDir
for your use case Nuxt Configuration: hooks.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@1290550861109657600> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai how can I access this in a util?
Let me check the knowledge base for you again. One moment! :books:
----------------------
The knowledge sources do not explicitly describe how to access the Nuxt root directory (
You should retrieve
rootDir
) from a utility function outside of hooks or the nuxt.config
context. The documented approach is to access builder.nuxt.options.rootDir
within Nuxt hooks in nuxt.config
Nuxt Configuration: hooks.
If you need to use the root directory in a utility, you would typically need to pass it as an argument from the hook or module setup where you have access to the Nuxt instance. There is no information in the knowledge sources about a global or static way to access rootDir
from arbitrary utility files.
Summary:You should retrieve
rootDir
from the Nuxt instance in a hook or module, then pass it to your utility as needed. There is no documented way to access it directly from a utility without this context.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@1290550861109657600> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
😦 anyone knows something about this?
What’s your goal here? You’re creating a directory… but what purpose does it serve for the util? Also, am I correct in assuming this is a server util?
@Cue Sorry didn't answer sooner T_T
Well I'm learning nuxt/vue trying stuff. This was for saving some user data to different json files and I wanted to have the files in a directory in the project directory itself 😄 Ik it's not good but it's just to learn how nuxt/vue works anyways.
@zigglearner1235 you would want to use something like a kv (key-value) store for that. Your current approach assumes that your server will always be on the same (single) machine, and that it would never grow. As soon as you need a second server (which you should have anyways for redundancy), the local file approach would fall apart. You can use the filesystem for testing, but read up on https://nuxt.com/docs/guide/directory-structure/server#server-storage to see how nuxt/nitro handles it in a pluggable way (you can swap in a service/tool like redis or upstash)
Nuxt
server · Nuxt Directory Structure v3
The server/ directory is used to register API and server handlers to your application.
👍