title: Elegant Dotenv Preloading w/ package.json knex scripts
body: <p>I am trying to find a more elegant way to preload my dotenv variables when running knex commands in my package.json scripts.</p>
  <p>I was able to find a working solution <a href="https://github.com/knex/knex/issues/2624" rel="nofollow noreferrer">here</a> but it seems more convoluted than it needs to be and want to understand either why this is needed or if there is a better way to accomplish the same thing.</p>
  <p><a href="https://github.com/WIL-what-i-learned/ExpressValidators" rel="nofollow noreferrer">CODEBASE - 100% docker</a></p>
  <hr />
  <p>Here are my <code>package.json</code> scripts</p>
  <pre><code>  &quot;scripts&quot;: {
      &quot;dev&quot;: &quot;node index.js&quot;,
      &quot;dev:watch&quot;: &quot;nodemon index.js&quot;,
  
      &quot;migrate:make&quot;: &quot;node -r dotenv/config ./node_modules/knex/bin/cli.js migrate:make --knexfile database/knexfile.js&quot;,
      &quot;migrate&quot;: &quot;node -r dotenv/config ./node_modules/knex/bin/cli.js migrate:latest --knexfile database/knexfile.js&quot;,
      &quot;migrate:rollback&quot;: &quot;node -r dotenv/config ./node_modules/knex/bin/cli.js migrate:rollback --knexfile database/knexfile.js&quot;,
      
      &quot;seed:make&quot;: &quot;node -r dotenv/config ./node_modules/knex/bin/cli.js seed:make --knexfile database/knexfile.js&quot;,
      &quot;seed&quot;: &quot;node -r dotenv/config ./node_modules/knex/bin/cli.js seed:run --knexfile database/knexfile.js&quot;
    },
  </code></pre>
  <p><strong>What I don't understand is why the <code>./node_modules/knex/bin/cli.js</code> is needed</strong> because none of the knex scripts work without it.</p>
  <hr />
  <p><strong>Relevant Files</strong></p>
  <p>database &gt; knexfile</p>
  <pre><code>require('dotenv').config();
  module.exports = {
      local: {
          client: 'postgres',
          connection: {
              host: 'db',
              user: process.env.PRODUCTION_POSTGRES_USER,
              password: process.env.PRODUCTION_POSTGRES_PASSWORD,
              database: process.env.PRODUCTION_POSTGRES_DB
          },
          migrations: {
              directory: __dirname + '/migrations',
          },
          seeds: {
              directory: __dirname + '/seeds',
          }
      },
      testing: {
          client: 'postgres',
          connection: {
              host: 'db_test',
              user: process.env.TEST_POSTGRES_USER,
              password: process.env.TEST_POSTGRES_PASSWORD,
              database: process.env.TEST_POSTGRES_DB
          },
          migrations: {
              directory: __dirname + '/migrations',
          },
          seeds: {
              directory: __dirname + '/seeds',
          }
      }
  }
  </code></pre>
  <p>database &gt; index.js</p>
  <pre><code>require('dotenv').config();
  const knex = require('knex')
  const knex_config = require('./knexfile')
  
  module.exports = knex(knex_config[process.env.NODE_ENV])
  </code></pre>