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

95
Views
Module.exports returns undefined

I'm trying to export a variable from a file, but when I require it in another I get undefined. I suspect my functions are causing it, but I'm not sure how to fix it.

Index.js:

app.post("/", (req,res) =>{ 
    console.log("Get from /");
    module.exports.SimpleMessage = 'Hello world';                 //exporting variable
    exec('npx hardhat run scripts/deploy.js --network goerli',
        (error, stdout, stderr) => {
            if (error !== null) {
                console.log(`exec error: ${error}`);
            }
        });
    res.send("Server received the request");
                            });

// starting the server
app.listen(3000, () => {
  console.log('listening on port 3000');
});

Deploy.js:

async function main() {
    const HelloWorld = await ethers.getContractFactory("HelloWorld");
 
    var msg = require('../src/index.js');                              //requiring variable
    console.log(msg.SimpleMessage);           

    const hello_world = await HelloWorld.deploy();   
 }

(Edit) This is the whole Deploy.js function:

async function main() {
    const HelloWorld = await ethers.getContractFactory("HelloWorld");
 
    var msg = require('../src/index.js');                              //requiring variable
    console.log(msg.SimpleMessage);           

    const hello_world = await HelloWorld.deploy();   
 }
 
 main()
   .then(() => process.exit(0))
   .catch(error => {
     console.error(error);
     process.exit(1);
   });
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

so there are a few things to unwrap over here:

  • your require() is probably invoked before module.exports.SimpleMessage = 'Hello world'; - an easy issue to encounter when you deal with async functions

  • require() is implemented in such a way that if you call it twice with the same file, the second time around only the cached value is returned and so, if the value of SimpleMessage changes, you will not be able to import it any more. Instead only the original value will be returned.

A quick and simple solution is to export a value that will not change instead, like a function

let SimpleMessage;

module.exports.getSimpleMessage = () => SimpleMessage;

app.post("/", (req,res) =>{ 
    console.log("Get from /");
    SimpleMessage = 'Hello world';

    // and the rest of your logic
});

at this point you can reference getSimpleMessage that will be cached by require() but still will always return the updated version of SimpleMessage

about 4 years ago · Santiago Gelvez 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!