Is there an idiomatic way to disable a module?
I have a module that I would like to not load. It's local to the project in the modules folder, so it auto-loads on app startup. I use the
hooks
object in the module defintion. As far as I can tell, there is no 'enabled' setting at the module level so you would need to implement it yourself. Why is that? What controls are in place to say hey, don't load this module?4 Replies
Hi @dmarr. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
You're right that there isn't a built-in "enabled" setting at the module level for local modules in the
modules/
directory that auto-load. Let me explain the current approaches for controlling module loading in Nuxt.
Controlling Local Module Loading
For modules in the modules/
directory that auto-load, there are a few approaches you can take:
1. Rename or move the module: The simplest approach is to rename the module directory or file so it doesn't match the auto-registered patterns (modules/*/index.ts
or modules/*.ts
). You could prefix it with an underscore (e.g., _disabled-module/
) or move it to another directory.
2. Implement your own conditional loading: You can implement conditional loading within the module itself using environment variables or runtime config.
Why There's No Built-in Enable/Disable
The Nuxt module system is designed with different approaches for external vs. local modules:
1. External modules are explicitly added to the modules
array in nuxt.config.ts
, so you control their inclusion directly.
2. Local modules in the modules/
directory are auto-loaded by convention as described in the documentation. This is meant for modules you're actively developing as part of your application.
The auto-loading of local modules is a convenience feature, with the assumption that if a module is in the modules/
directory, you intend to use it. The module system doesn't have a built-in "enabled" flag because:
1. For external modules, you simply remove them from the modules
array in nuxt.config.ts
2. For local modules, the expectation is that you would remove or rename them if you don't want them to load
Alternative Approaches
If you're frequently toggling modules on and off during development, you might consider:
1. Using the modules array instead: Move your local module to another directory and reference it explicitly in your nuxt.config.ts
file, which gives you direct control over when it's included.
2. Using a naming convention: You could adopt a naming convention like prefixing disabled modules with an underscore or "disabled-".
3. Implementing a module manager: Create a custom module that conditionally loads other modules based on configuration.
The Nuxt module system is designed to be simple and convention-based, which is why there isn't a built-in enable/disable flag for auto-loaded modules.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@139883350096740354> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Most modules with such functionality feature
enabled
or disabled
option. As you said, this must be implemented by the module itself. It is easy enough, I do not think there is a real need for easier way. Example: https://github.com/harlan-zw/nuxt-schema-org/blob/main/src/module.ts#L94-L97Thanks Mole, I was curious about the hooks that are declared with the object syntax (alongside the setup function). It seems those would always run as the config isn’t available from what I can tell.