Skip to content

nodejs import dynamic variable from another file

by using module.exports = var; and const var = require("./file.js"); we can access a variable from another file but the imported variable is static and cannot change even if the original variable changes in the original file, how can I export an array that can be updated at any time and accessible in real time in another file?



put your variable inside of a function that returns the variable then export the function

export function getVariable(){
  let myVar = 0;
  return myVar
module.exports = getVariable;

const getVar = require('../file.js');