Question

I'm still having some trouble understanding the scope of variables inside a node.js app. In regular javascript a nested function has access to the variables of it's parents. In node, this doesn't seem the case.

Here i have the file app.js that has a variable 'bookies' and a require 'routes'. The trouble i'm having is that the variable 'bookie' is not available in my routes.js file.

Why is that?

enter image description here

Was it helpful?

Solution

If you want to have bookie defined in the routes require you should pass the bookie like this:

var routes = require("./routes")(bookie);

This is because in Node, The top-level scope is not the global scope; var something inside a Node module will be local to that module.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top