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

286
Visualizações
Is there a way to get a variable or function from one file to another file in javascript?

I have a file for my Blockchain called gale.js, and I need the Blockchain variable to get into my miner.js file:

let galeCoin = new Blockchain(); // My Blockchain

Can I access this variable from my miner.js file so I can run the minePendingTransactions() function from the Blockchain class

var galeCoin = /* the coin imported from the gale.js file */
var minerAdress = "0xOTUYboIUYEO5C274OUIYGo8isYOCYW6o87TO"; // SHA256 Hash for the miner
while (true) {
  minePendingTransactions(minerAdress); // mine (forever)
}

I would also like to access this variable in my user.js file:

var galeCoin = /* the coin imported from the gale.js file */
var userAdress = galeCoin.newAdress(); // A new adress for the user

// I will add the code here later...

And many other files!

But I can't seen to access the variable from the other files! I tried this:

let galeCoin = new Blockchain();

export { galeCoin };
import { galeCoin } from './gale.js'

but that gives me this error when I run miner.js using node miner.js:

node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module 'gale.js'
Require stack:
- /Users/Movies/Desktop/devProjects/Blockchain/SecondTry/miner.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/Movies/Desktop/devProjects/Blockchain/SecondTry/miner.js:1:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/Users/Movies/Desktop/devProjects/Blockchain/SecondTry/miner.js' ]
}

I dont understand the error message

I need help as soon as posible!

NOTE: My files are in /Users/Movies/Desktop/devProjects/Blockchain/SecondTry/

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try :

export let galeCoin = new Blockchain(); 
//add export statement at the beginning of the line where you declare your variable

You can read about it here: https://javascript.info/import-export

about 4 years ago · Juan Pablo Isaza Relatório

0

Try using the following to import galeCoin from gale.js:

var {galeCoin} = require('./gale.js');

You'll also need to export the galeCoin object from gale.js. You can use the following (append somewhere at the end of gale.js):

module.exports = {galeCoin};

Then you're going to want to run npm install gale.js to ensure that module is added to the node_modules directory. For this you're going to want to make sure the node_modules is in the same director as miner.js. If it is not, npm -g install gale.js.

I should note, I'm assuming you're using node.js, which from the error you threw, seems to be the case.

I hope this was helpful:)

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