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

217
Visualizações
Nodejs Question about circular dependency

I was testing circular dependency with commonjs.

//index.js
console.log('main starting');
const a = require('./a.js');
const b = require('./b.js');
console.log('in main, a.done = %j, b.done = %j', a.done, b.done);
//a.js
console.log('a starting');
exports.done = false;
const b = require('./b.js');
console.log('in a, b.done = %j', b.done);
exports.done = true;
console.log('a done');
//b.js
console.log('b starting');
exports.done = false;
const a = require('./a.js');
console.log('in b, a.done = %j', a.done);
exports.done = true;
console.log('b done');

The result was

// output
main starting
a starting
b starting
in b, a.done = false
b done
in a, b.done = true
a done
in main, a.done = true, b.done = true

So index import a and a import b

b try to import a but a is not finished so a return unfinished module.

OK fine, I understood.


But what about esmodule syntax ?

I reproduced code like this.

//index.js

console.log('main starting');
import a from './a.js';
import b from './b.js';
console.log('in main, a.done = %j, b.done = %j', a.done, b.done);
//a.js
console.log('a starting');
export let done = false;
import {done as B_done}  from './b.js';
console.log('in a, b.done = %j', B_done);
done = true;
console.log('a done');
export default {
    done
}
//b.js
console.log('b starting');
export let done = false;
import {done as A_done} from './a.js';
console.log('in b, a.done = %j', A_done);
done = true;
console.log('b done');
export default {
    done
}

But the result was changed.

ReferenceError: Cannot access 'A_done' before initialization

This error occrued.

Why is it giving me a different result than commonjs ?

Doesn't esmodule return unfinished modules?

You can actually find this code sample in here

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

0

Doesn't esmodule return unfinished modules?

It does. Try with var instead of let.

Why is it giving me a different result than commonjs?

Because it also changed evaluation order. Imports are "hoisted", the evaluation of the module code is deferred until all dependencies are set up (that is, either completely finished evaluating, or having their variables declared and waiting for circular dependencies). Unlike the require() call, you won't get the initialisation code of b executing in the middle of a.

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