Conditional module require inside an "If" still executed no matter what in nuxt.config.js?

Viewed 142

somehow this "require" still executed and error because file mod.json does not exist, the condition should not have met and should not get into the "if" block:

const a = 1;
if(a === 2){
    const mod = require('./scripts/mod.json');
    ...
}

Any help and suggestion would be appreciated, thanks!

1 Answers

Try using the grave accent (`) instead.

If it still doesn't work, try something like:

const a = 1;
if(a === 2){
    const path = './scripts/mod.json'
    const mod = require(`$path`);
    ...
}

If it keeps solving the require(), try defining the path variable somewhere else (like mounted, or created), so the compiler can't easily find it.

If it keeps accessing the require, you might try to make sure that a === 2

Related