Nuxt.js: Adding multiple times a same module in nuxt.config.js is not working

Created on 25 Jun 2020  路  2Comments  路  Source: nuxt/nuxt.js

Version

v2.13.1

Reproduction link

https://codesandbox.io/s/nuxt-module-issue-not-working-k40t5

Steps to reproduce

  • Fork the CodeSandbox (seems to be required for experimental features used by codesandbox-nuxt)
  • Go to Server Control Panel
  • Click "Restart Server" in Control Container
  • Look at Terminal under the Browser Preview after the line :
    log > Sandbox Container: Sandbox <xxxxx> started yarn run v1.22.4 [...]

What is expected ?

The foo module should be print twice the console.log defined in ~/modules/foo/index.js because nuxt.config.js contains two times '~/modules/foo' in options.modules, with different modules' options. See Additional comments for the output of the console.log.

What is actually happening?

The foo moduleprints only once the console.log.

Additional comments?

I've added a "foo" module that do a simple console.log for demo purpose. I expect it to be called twice, but it is not the case.

Reproduction link details

The console.log output for the NOT WORKING CodeSandbox (the one in the "Reproduction link") is :

[nuxt-v2.13.1] foo module being added, options: {
  foo: 'bar'
}

A working example

Here found a WORKING CodeSandbox using [email protected] : https://codesandbox.io/s/nuxt-module-issue-working-lnzpc

The console.log output for the WORKING CodeSandbox is :

[nuxt-v2.12.2] foo module being added, options: {
  foo: 'bar'
}

[nuxt-v2.12.2] foo module being added, options: {
  bar: 'baz'
}

Real use case

The real use case is a module copying files base on a template. The copy is done using addTemplate with the same source file to a different destination and options (based on module options). Each time the module is referenced in the nuxt.config.js, I need to execute the module in order to generate needed files.

This bug report is available on Nuxt community (#c10845)
available soon bug-report

All 2 comments

Hi @gtnsimon. I've added fix to avoid breaking change (d0056fb) will be released with next patch release to only warn.

You can import module to workaround regression and avoid error:

import foo from './modules/foo'

export default {
  modules: [
    [foo, { foo: 'bar' }],
    [foo, { bar: 'baz' }]
  ]
}

But I would highly recommend instead, supporting something like templates: [] option so that module can handle multiple copies in one executation.

Hi @pi0. Thank you for the tip on the use case. I'll refactor my code to fit to the new version, I wasn't aware on bests practices on how modules should be added to nuxt.config.js.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pehbehbeh picture pehbehbeh  路  3Comments

maicong picture maicong  路  3Comments

bimohxh picture bimohxh  路  3Comments

o-alexandrov picture o-alexandrov  路  3Comments

lazycrazy picture lazycrazy  路  3Comments