Nuxt Module require other libraries without installing them
I am creating custom Nuxt module and I want to make it depend on
zod
and @nuxt/ui
, but not install them. I don't want to be something like @nuxt/ui
that installs @nuxt/fonts
instantly.
I want that, because I don't want to release often version updates when for example new version of zod
is released. Is it even possible? Are there modules that have optional logic when other is detected?2 Replies
Hi @Norbiros. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Yes, it is possible to create a Nuxt module that depends on other libraries (like
This way, your module will not install them automatically. - Import and use the libraries in your module code.
If the user hasn't installed them, it will throw an error at runtime. You can catch this error and provide a helpful message, or implement optional logic if the dependency is not present. - Check for the presence of a dependency:
You can use
Many modules use this pattern to provide optional integration with other modules or libraries. Example pattern: About modules like
This is done intentionally by calling
- You can require peer dependencies and document them for your users. - You can implement optional logic if a dependency is present. - You are not forced to auto-install dependencies like some Nuxt modules do; this is a design choice. The knowledge sources confirm the use of
zod
or @nuxt/ui
) without installing them directly as dependencies of your module. Instead, you can require users to install these dependencies themselves in their project. This approach is common for optional or peer dependencies.
How to handle this in your Nuxt module:
- Do not add the libraries to your module's dependencies
in package.json
.This way, your module will not install them automatically. - Import and use the libraries in your module code.
If the user hasn't installed them, it will throw an error at runtime. You can catch this error and provide a helpful message, or implement optional logic if the dependency is not present. - Check for the presence of a dependency:
You can use
try/catch
with require()
or dynamic import()
to check if a package is available and only enable related features if it is.
- Optional logic based on detection:Many modules use this pattern to provide optional integration with other modules or libraries. Example pattern: About modules like
@nuxt/ui
installing @nuxt/fonts
:This is done intentionally by calling
installModule
within the module's setup, which you can avoid in your own module. If you don't want to auto-install dependencies, simply do not call installModule
for them in your module code. Let the user decide which modules to install and configure in their own nuxt.config.ts
see example of using installModule
.
Summary:- You can require peer dependencies and document them for your users. - You can implement optional logic if a dependency is present. - You are not forced to auto-install dependencies like some Nuxt modules do; this is a design choice. The knowledge sources confirm the use of
installModule
for auto-installing dependencies, but you are not required to use it. Optional logic based on detection is a standard Node.js/JavaScript pattern, though the sources do not provide a direct example of this for Nuxt modules specifically Using Other Modules in Your Module, installModule API.
If you need to support optional logic, you can safely use the try/catch
pattern as shown above.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@770620808644919307> Kapa.ai is still learning and improving, please let me know how I did by reacting below.