sycoding
sycoding

Reputation: 39

node.js dynamic module(file) export

how can I exports this dynamic module?

// ./data/example5.js
module.exports = {title : example5, recentCheck : "2018-08-22"}


The contents change in real time. And I execute the followed function once a minute and connect the module.

    var files = fs.readdirSync(`./data/`);
    var i = 0;
    var link = [];

    while(i<files.length){ // read all file

        link[i] = require(`../data/${files[i]}`);

 

//main.js
setInterval(start,10000);  

I try to create and connect a new module file once a minute, but the first stored file(module) is extracted. The modulefile is being saved in real time correctly.

Shutting down and running the node will extract the changed modules correctly.

How do I handle a dynamically changing module?

Upvotes: 2

Views: 1030

Answers (2)

LJD
LJD

Reputation: 496

Just make the objects you're updating variables in the module that you're including. Make a function called getVariable, and simply return the variable.

Include getVariable in your main module.

Upvotes: 1

RedJandal
RedJandal

Reputation: 1213

I would recommend saving the data in a JSON file and then reading the data from the file rather than trying to use it as a module.

Upvotes: 1

Related Questions