K
K
KG2019-10-16 00:57:56
Node.js
KG, 2019-10-16 00:57:56

How to connect the necessary module in js?

Hello, I ran into a problem to connect a specific module in js.
There is a configs folder, where there are different js naming files site-name1-config.js, site-name2-config.js and so on. There is a folder with scripts, where the main.js file is located. At the beginning, a variable is set there, the name of the site, let's say site-name1.
So, the task is to load site-name1-config.js from configs into this main.js.
I tried using module.exports and require, but in the path inside require you cannot forward a variable like require('../configs/'+siteName+-config.js''). Can you tell me how else to solve this problem? Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pigeon1235, 2019-10-16
@Pigeon1235

const site_1 = fs
  .readdirSync(`${__dirname}/configs/`)
  .filter((name) => site-name1-config\.js$/i.test(name))
  .map((name) => require(`${__dirname}/configs/${name}`));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question