Knex: Using knex with dotenv

Created on 4 Dec 2014  ·  19Comments  ·  Source: knex/knex

Hi,
is there a simple way to use Knex and the migration/seed CLI with dotenv?

Most helpful comment

In case anyone comes here looking for a solution, here's what I do (and it works like charm!)

require('dotenv').config({path: 'path_to_env_file'});

module.exports = {
  client: 'pg',
  connection: {
    host: process.env.DB_HOST,
    user: process.env.DB_USER,
    password: process.env.DB_PASS,
    database: process.env.DB_NAME
  }
};

All 19 comments

You'd need to create a knexfile, call dotenv at the top (https://github.com/motdotla/dotenv#usage) and then set up your configuration based on process.env.

I guess I'd have to do something like this to populate the right key of the exports hash?

var dotenv = require('dotenv');
dotenv.load();

module.exports[process.env.NODE_ENV] = {
    client: 'mariasql',
    connection: {
        host: process.env.DB_HOST,
        user: process.env.DB_USERNAME,
        password: process.env.DB_PASSWORD,
        db: process.env.DB_NAME,
        charset: 'utf8'
    },
    pool: {
        min: 2,
        max: 10
    },
    migrations: {
        tableName: 'knex_migrations'
    }
};

Correct

That didn't work, migrations still look for the default environment (development) no matter what I set.

See #527

There hasn't been a release since I merged but that's ideally what you want. Environments are a weird feature and the better solution is to use a configuration hierarchy tool (dotenv being a simple one). For now just set all the environments to the same object.

exports.development = exports.production = exports.otherEnv = configObject;

In case anyone comes here looking for a solution, here's what I do (and it works like charm!)

require('dotenv').config({path: 'path_to_env_file'});

module.exports = {
  client: 'pg',
  connection: {
    host: process.env.DB_HOST,
    user: process.env.DB_USER,
    password: process.env.DB_PASS,
    database: process.env.DB_NAME
  }
};

@mbavio Thanks that fixed it the issue. The problem for me specifically was that I have my knexfile.js is a subdirectory of my project, not at the root directory. So therefore it couldn't find my .env without passing the path to it. Thanks again!

I have the problem when I want to run knex migrate:currentVersion it is not loading from .env file. what can I do to resolve that?

I had the same problem when I tried to run migrations as npm scripts. It worked when I changed the script to node -r dotenv/config ./node_modules/knex/bin/cli.js migrate:latest.

Hi, everyone It's possibile to run knex in silent mode, so without asking me via prompt the environment , I'm deploying in a different machine via ssh

@kcheang I have trouble to use that approach with a generic command, especially when I need to add a dotenv_config_path parameter.

With Babel (ES6) you can do:

```javascript
import dotenv from 'dotenv';
dotenv.config();

export default require('knex')({
debug: true,
client: 'mysql',
connection: {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PW,
database: process.env.DB,
charset: 'utf8',
}
});

@gerbus Won't that be a problem when using migrations and seeds?

I had the same problem when I tried to run migrations as npm scripts. It worked when I changed the script to node -r dotenv/config ./node_modules/knex/bin/cli.js migrate:latest.

You are f*cking genius

You may find this helpful:
env $(cat .env) knex migrate:latest

And if you don't have knex installed globally then:
env $(cat .env) ./node_modules/knex/bin/cli.js migrate:latest

@RaimoJohanson A more proper way to do that would be

env $(cat .env) ./node_modules/.bin/knex migrate:latest

or if you are using an NPM Script:

env $(cat .env) knex migrate:latest (the .bin directory is automatically resolved when using npm scripts)

@Jakobud I read up about the .bin directory being used by Node internally in npm scripts - thanks for that!

@Jakobud this wouldn't work on Windows

With multiple .env files it might be a solution to use the DOTENV_CONFIG_PATH environment variable. In my package.json I created a knex script that picks up .env, and a test:knex that works with .env.test:

"scripts": {
  "knex": "babel-node -r dotenv/config node_modules/.bin/knex",
  "test:knex": "DOTENV_CONFIG_PATH=.env.test npm run knex"
}
Was this page helpful?
0 / 5 - 0 ratings