Using imports conditionally without import()

Viewed 34

This may sound weird but I need to use imports in a conditional way and without using await import() .

Here's a quick example, lets say I have this code:

import { en } from './langs/en.json';
import { fr } from './langs/fr.json';  

And I want to conditionally use en or fr.
Something like:

const selectedLang = "en";
const lang = global[selectedLang]; 

Is there a way of doing so?
I know that under the browser all global variables are stored in the window object.
But it is not the case with this library.

1 Answers

You could use plain old require for this:

const selectedLang = "en";
const lang = require(`./langs/${selectedLang}.json`);
Related