Conditional module loading in Nuxtjs

How to conditionally load a module in NuxtJS ?

Published 2021-07-16

Ever tried to load a specific module in NuxtJS depending on some conditions? You probably tried to use a ternary operator or an arrow function into the modules Array located in your nuxt.config.js ... BUT if the condition is not met, you will get an error because you NEED to return a String with a valid Path.

So here is the trick: create an Array outside of the export, and push your module IF the condition returns true, THEN you can use this variable for your modules Array.

const modules = [
  '...',
  '...',
  '...',
  '...',
  '...',
  '...'
]

if (process.env.SOMETHING)
  modules.push(['some-module', {
    option: 'some-options',
  }])

export default {
// ...

    modules,
    
// ...

}

Related Tutorials :