title: Undefined db connection with knex
body: <p>I am executing the following script, <code>node acl.js</code>:</p>
  
  <p><strong>acl.js</strong></p>
  
  <pre><code>require('dotenv').config()
  
  const _ = require('lodash');
  const buckets = require('./buckets');
  const knex = require('../src/config/db'); //HERE I am getting the ERROR
  
  var downSql = 'DROP TABLE IF EXISTS "{{prefix}}{{meta}}";'+
      'DROP TABLE IF EXISTS "{{prefix}}{{resources}}";'+
      'DROP TABLE IF EXISTS "{{prefix}}{{parents}}";'+
      'DROP TABLE IF EXISTS "{{prefix}}{{users}}";'+
      'DROP TABLE IF EXISTS "{{prefix}}{{roles}}";'+
      'DROP TABLE IF EXISTS "{{prefix}}{{permissions}}";';
  var upSql = 'CREATE TABLE "{{prefix}}{{meta}}" (key TEXT NOT NULL PRIMARY KEY, value TEXT[][] NOT NULL);'+
      'INSERT INTO "{{prefix}}{{meta}}" VALUES (\'users\', \'{}\');'+
      'INSERT INTO "{{prefix}}{{meta}}" VALUES (\'roles\', \'{}\');'+
      'CREATE TABLE "{{prefix}}{{resources}}" (key TEXT NOT NULL PRIMARY KEY, value TEXT[][] NOT NULL);'+
      'CREATE TABLE "{{prefix}}{{parents}}" (key TEXT NOT NULL PRIMARY KEY, value TEXT[][] NOT NULL);'+
      'CREATE TABLE "{{prefix}}{{roles}}" (key TEXT NOT NULL PRIMARY KEY, value TEXT[][] NOT NULL);'+
      'CREATE TABLE "{{prefix}}{{users}}" (key TEXT NOT NULL PRIMARY KEY, value TEXT[][] NOT NULL);'+
      'CREATE TABLE "{{prefix}}{{permissions}}" (key TEXT NOT NULL PRIMARY KEY, value JSON NOT NULL);';
  
  function tmpl(str, ctx) {
      var n = 1;
      var sql = str.replace(/{{(\w+)}}/g, function(match, cap1) {
          return ctx[cap1] || match;
      });
      return sql.replace(/\?/g, function() { return '$' + n++; });
  }
  
  function createTables(callback) {
      var prefix = ''
      var bucketNames = buckets(args[8])
  
      if (!prefix) prefix = 'acl_';
  
      knex.raw(tmpl(downSql+upSql, {
              'meta': bucketNames.meta,
              'parents': bucketNames.parents,
              'permissions': bucketNames.permissions,
              'prefix': prefix,
              'resources': bucketNames.resources,
              'roles': bucketNames.roles,
              'users': bucketNames.users
          }))
          .then(function() {
              if (!_.isUndefined(callback)) {
                  callback(null, db);
              }
          })
      ;
  }
  
  createTables()
  </code></pre>
  
  <p><strong>buckets.js</strong></p>
  
  <pre><code>'use strict';
  
  var _ = require('lodash');
  
  var buckets = function(options){
      return _.extend({
          meta: 'meta',
          parents: 'parents',
          permissions: 'permissions',
          resources: 'resources',
          roles: 'roles',
          users: 'users'
      }, options);
  };
  
  exports = module.exports = buckets;
  </code></pre>
  
  <p>When I run <code>node acl.js</code>  I get the following error:</p>
  
  <pre><code>&gt; node acl.js
  { development:
     { client: undefined,
       connection: { user: undefined, password: undefined, database: undefined } } }
  undefined
  C:\Users\admin\Desktop\Coding Projects\learning_npm_node_acl\node_modules\knex\lib\index.js:49
    if (arguments.length === 0 || !config.client &amp;&amp; !config.dialect) {
                                         ^
  
  TypeError: Cannot read property 'client' of undefined
      at Knex (C:\Users\admin\Desktop\Coding Projects\learning_npm_node_acl\node_modules\knex\lib\index.js:49:40)
      at Object.&lt;anonymous&gt; (C:\Users\admin\Desktop\Coding Projects\learning_npm_node_acl\src\config\db.js:8:29)
      at Module._compile (module.js:573:30)
      at Object.Module._extensions..js (module.js:584:10)
      at Module.load (module.js:507:32)
      at tryModuleLoad (module.js:470:12)
      at Function.Module._load (module.js:462:3)
      at Module.require (module.js:517:17)
      at require (internal/module.js:11:18)
      at Object.&lt;anonymous&gt; (C:\Users\admin\Desktop\Coding Projects\learning_npm_node_acl\node_acl\acl.js:5:14)
      at Module._compile (module.js:573:30)
      at Object.Module._extensions..js (module.js:584:10)
      at Module.load (module.js:507:32)
      at tryModuleLoad (module.js:470:12)
      at Function.Module._load (module.js:462:3)
      at Function.Module.runMain (module.js:609:10)
  </code></pre>
  
  <p>The error comes from the line <code>const knex = require('../src/config/db');</code> in my <code>acl.js</code> file.</p>
  
  <p>My <code>db.js</code> file looks like the following:</p>
  
  <pre><code>require('dotenv').config()
  // Loading from an external file
  const config = require('../../knexfile')
  console.log(config)
  const env = process.env.DB_ENV
  console.log(env)
  
  const knex = require('knex')(config[env])
  
  knex.on('query', (queryData) =&gt; {
    console.log(queryData)
  })
  
  module.exports = knex
  </code></pre>
  
  <p>As you can see the <code>console.log</code> statements give back mostly <code>undefined</code> in the above shown output.</p>
  
  <p>The <code>knexfile.js</code> is loading the configuration from a <code>.env file</code></p>
  
  <p><strong>knexfile.js</strong></p>
  
  <pre><code>require("dotenv").config()
  
  module.exports = {
  
      development: {
          client: process.env.DB_CONNECTION,
          connection: {
              user: process.env.DB_USERNAME,
              password: process.env.DB_PASSWORD,
              database: process.env.DB_DATABASE,
          },
      },
  }
  </code></pre>
  
  <p><strong>.env</strong></p>
  
  <pre><code># Database
  DB_CONNECTION=postgresql
  DB_ENV=development
  DB_USERNAME=root
  DB_PASSWORD=root
  DB_DATABASE=nodeacl
  </code></pre>
  
  <p>When I run <code>knex migrate:latest</code> or <code>knex seed:run</code> the seeds and migration files run perfectly.</p>
  
  <p>Any suggestions why I am getting the above shown error?</p>