Build hook - dev only
I am trying to create pre-build script for my Nuxt app.
So I have chosen defining build:before hook in nuxt.config.ts. The reason is copying files from mother dest. How can I only run this hook in development mode (npm run dev)? import.meta.dev is undefined there.
5 Replies
Hi @PetyXbron. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To conditionally run a build hook like
build:before only in development mode within your nuxt.config.ts, you should check the Nuxt context's dev property, not import.meta.dev. The import.meta.dev property is only available at runtime (in your app code or plugins), not in the Nuxt config or module setup context, which is executed at build time.
Here's how you can do it:
Alternatively, if you are writing a Nuxt module, you can access the nuxt.options.dev property:
Key points:
- import.meta.dev is not available in nuxt.config.ts or module setup—it's for runtime code only Import meta.
- Use process.env.NODE_ENV or, in modules, nuxt.options.dev to check for development mode in build hooks Nuxt Configuration.
If you need to run code only in dev mode, wrap your logic in a conditional as shown above.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@411436203330502658> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai what are other possible values of
process.env.NODE_ENV?Let me check the knowledge base for you again. One moment! :books:
----------------------
The knowledge sources indicate that the most common values for
process.env.NODE_ENV in Nuxt are:
- development
- production
- (and sometimes) custom values like staging or testing
For example, the documentation and several code samples show usage of process.env.NODE_ENV === 'production' and process.env.NODE_ENV === 'development' to distinguish between development and production environments Nuxt Configuration. There are also references to using custom values such as staging (e.g., setting NODE_ENV to stage on Heroku) Stack Overflow - Using different api url from .env based on environment in Nuxt 3.
However, the knowledge sources do not provide an exhaustive list of all possible values. They do show that you can set NODE_ENV to any string value you want for your own environment-specific logic, but development and production are the standard ones recognized by Nuxt and its tooling.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@411436203330502658> Kapa.ai is still learning and improving, please let me know how I did by reacting below.