Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda