2

I have a node file used in express which can not see a required file within a function , but when setting breakpoints, I can see it defined right after the declaration. The other variable, auth can be seen fine in both places

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

And I'm pretty sure I have the paths correct. The snippet above is from user.js enter image description here

a more complete snippet is here . https://gist.github.com/foobar8675/eb5ec78461dff59a80d1

Any suggestions are appreciated!

Matthew Chung
  • 1,322
  • 1
  • 19
  • 31

1 Answers1

1

I would be wary of using the name index.js in this context as it has special significance when modules are resolved. index.js would normally be called when require is passed a folder name, i.e.

var utilities = require('../utilities');

Can't be sure, but try changing the name of the file to something else like indexhelper.js and see what happens.

Update

I just ran a test in response to your screencast and I think I can now see your problem. Your invisible require vars are not referenced inside the module.exports scope and are thus not being captured. I just ran a test with the following snippet and saw the exact same phenomenon inside the debugger.

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")
    }
};

Webstorm debugger snapshot

So in summary. I don't really think you have a problem here. Just reference the variable inside module.exports and it will be captured.

See also: In what scope are module variables stored in node.js?

Community
  • 1
  • 1
Tom Makin
  • 3,203
  • 23
  • 23
  • right now i'm just experimenting and there are some odd things going on. i made a screencast if anyone cares to look and has some can shed some wisdom. https://www.youtube.com/watch?v=drk4zxPJnCk&feature=youtu.be – Matthew Chung Dec 30 '15 at 17:28