title: NodeJs : Reference Error : (my own var) not defined
body: <p><strong>Trying to require and use a module in my router file. I have managed to require it (I think) but now it says the following error:</strong></p>
  
  <blockquote>
    <p>ReferenceError: USERS is not defined    at
    c:\work\nodejs\router\main.js:32:19    at Layer.handle [as
    handle_request]
    (c:\work\nodejs\node_modules\express\lib\router\layer.js:82:5)    at
    next (c:\work\nodejs\node_modules\express\lib\router\route.js:110:13) 
    at Route.dispatch
    (c:\work\nodejs\node_modules\express\lib\router\route.js:91:3)    at
    Layer.handle [as handle_request]
    (c:\work\nodejs\node_modules\express\lib\router\layer.js:82:5)    at
    c:\work\nodejs\node_modules\express\lib\router\index.js:267:22    at
    Function.proto.process_params
    (c:\work\nodejs\node_modules\express\lib\router\index.js:321:12)    at
    next (c:\work\nodejs\node_modules\express\lib\router\index.js:261:10) 
    at c:\work\nodejs\node_modules\body-parser\lib\read.js:111:5    at
    IncomingMessage.onEnd
    (c:\work\nodejs\node_modules\body-parser\node_modules\raw-body\index.js:136:7)</p>
  </blockquote>
  
  <p><strong>where USERS is an object containing an array of objects as follows:</strong></p>
  
  <pre><code>//users.js
  module.exports = function () {
  
  // Create User prototype and objects
  var USERS = { users: [] };
  
  function User(type, useremail, password) {
   this.type = type;
   this.useremail = useremail;
   this.password = password;
  }
  
  var Bob = new User("rep", "bob@bob.com", "qwerty");
  USERS.users.push(Bob);
  
  var Helen = new User("rep", "helen@helen.com", "test");
  USERS.users.push(Helen);
  
  var Dominic = new User("customer", "dom@dom.com", "1234");
  USERS.users.push(Dominic);
  
  var James = new User("grower", "james@james.com", "pass1");
  USERS.users.push(James);
  
  };
  </code></pre>
  
  <p><strong>My router code is:</strong></p>
  
  <pre><code>var url = require('url'); var users = require('./router/users.js');
  
  module.exports = function (app) {
  
      app.get('/', function (req, res) {
          res.render('index.html');
          console.log("Home page displayed");
      });
  
  app.get('/login', function (req, res) {
          res.render('login.html');
          console.log("Log in page displayed");
  
      });
  
  app.get('/api/orders/:id', function (req, res) {
        console.log(req.params.id); 
        res.json(ORDER.orders[req.params.id]);
      });
  
      app.get('/api/orders/', function (req, res) {
        console.log(ORDER); 
        res.json(ORDER);
  
      });      app.get('/api/users/:id', function (req, res) {
        console.log(req.params.id); 
        res.json(USERS.users[req.params.id]);
      });
  
      app.get('/api/users/', function (req, res) {
        console.log(USERS); 
        res.json(USERS);
  
      });
  </code></pre>
  
  <p><strong>How do I correctly reference USERS in this case in my router?</strong></p>