Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
Changing same instance of variable from other file nodejs

I'm not sure how to formulate an explanation, but I want to know how I can change a variable from the index.js file in another file. I thought by doing module.exports=(thing you want to export) then requiring the file will allow me to access the same instance of that variable, but it seems like it returns undefined. Bare in mind, I'm new to nodejs and I want to learn.

in index.js:

const multiVerse={stuff};
module.exports=multiVerse;

in other js file I want to use this in:

const main = require(./index.js)
//change data of object
main.multiVerse;

question: how can I access a variable from the index file in some other place in the project?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const multiVerse = {foo: 'bar'};
module.exports = multiVerse;

// ...

const main = require('./index.js');
main.foo = 'baz';

or

const multiVerse = {foo: 'bar'};
module.exports = { multiVerse };

// ...

const main = require('./index.js');
main.multiVerse.foo = 'baz';

or

const multiVerse = {foo: 'bar'};
module.exports = { multiVerse };

// ...

const { multiVerse } = require('./index.js');
multiVerse.foo = 'baz';
about 4 years ago · Juan Pablo Isaza Report

0

you can export like this -

in index.js

    exports.variableA = 'some_value';

in other.js

    var indexExports = require('./index');
    ...
    ...
    indexExports.variableA = 'Any_value';
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!