我有一个在express中使用的节点文件,它无法在函数中看到所需的文件,但在设置断点时,我可以看到它在声明后立即定义。在两个地方都可以看到另一个变量auth
var auth = require('../utilities/auth');
var index = require('../utilities/index');
// here, i set a break point and index is defined
module.exports = {
create : function (req, res) {
// in here, i set a breakpoint, index is not defined
而且我很确定我的路径是正确的。上面的代码片段来自user.js
这里有一个更完整的代码段。 https://gist.github.com/foobar8675/eb5ec78461dff59a80d1
任何建议都表示赞赏!
答案 0 :(得分:1)
我会谨慎地在此上下文中使用名称index.js
,因为它在解析模块时具有特殊意义。当require传递文件夹名称时,通常会调用index.js
,即
var utilities = require('../utilities');
无法确定,但请尝试将文件名更改为indexhelper.js
之类的内容,看看会发生什么。
我刚刚对你的截屏进行了测试,我想我现在可以看到你的问题了。您的不可见require
变量未在module.exports范围内引用,因此未被捕获。我刚刚使用以下代码片段进行了测试,并在调试器中看到了完全相同的现象。
var mod1 = require("./mod1");
var mod2 = require("./mod2");
//both mod1 and mod2 are visible here
module.exports = {
init : function() {
//mod1 not referenced so only mod2
//is available as a local scope variable in debugger
mod2.init();
console.log("module 3 initialised")
}
};
总结一下。我真的不认为你这里有问题。只需引用module.exports中的变量,它就会被捕获。