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

124
Views
Do variables defined in index.js transfer over to other scripts?

I have index.js running the following code:

var ClassData=require("./server/classdata.js");
var data=require("./server/data.js");

The classdata.js file contains multiple classes, and I was wondering if there is any way to get that into data.js. As of now, the following code is:

var data={};

console.log("hello1"); //this gets logged
module.export=data;
console.log("hello2"); //this gets logged

console.log(ClassData); //"ClassData is not defined"

The data object gets exported when the last line is not there, otherwise with it there it says that ClassData is not defined. I know I can probably define it using the same way I did in index.js, using require();, but I would think that makes it run the whole script again. Is there a way to just get it straight from data.js such that it is defined? Would I need to put it in the data.require part? I don't care if I can't change ClassData.

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

0

The classdata.js file contains multiple classes, and I was wondering if there is any way to get that into data.js

You can use require('./classdata.js') inside of data.js, that is what require is for, reusing a module inside another.

I know I can probably define it using the same way I did in index.js, using require();, but I would think that makes it run the whole script again.

Modules imported with require are cached after their first use so the script will not run again.

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!