0

Let's say I have module A.js and B.js.

A.js

var b = require('./B');
[...some code here...]

B.js

var a = require('./A');
[...some code here...]

than in my app.js I have something like:

app.js

var a = require('./A');
[some code here]

The thing is that the var a in B.js is always an empty object {} when I do like node app.js while If I directly do node B.js it is properly initialized.

What instead I would expect is that calling node app.js it triggers A.js (that requires B.js) and so, in turn it initialtes its own a variable.... but it is not like this apparently....

4

1 回答 1

2

你有一个循环模块依赖,所以顺序是这样的:

  1. app.js需要一个
  2. A 需要 B
  3. B 需要 A(尚未完全定义)

在第 3 步中,B 在请求时获取 A 的定义。那时这只是一个空对象,所以这就是a设置为 in的内容B.js

于 2012-10-22T16:08:17.010 回答