20

Here are my files. knexfile.js

require('dotenv').config();
module.exports = {
      development: {
        client: process.env.DB_CLIENT,
        connection: {
          host: process.env.DB_HOST,
          user: process.env.DB_USER,
          password: process.env.DB_PASSWORD,
          database: process.env.DB_NAME
        },
        migrations: {
          directory: __dirname + '/db/migrations'
        },
        seeds: {
          directory: __dirname + '/db/seeds'
        }
      }
    };

knex.js

const environment = process.env.NODE_ENV || 'development';
let config = require('../knexfile')[environment];
module.exports = require('knex')(config);

index.js

require('babel-register');
import express from 'express';

const port = process.env.PORT || 5000;
const app = express();

app.listen(port, () => {
  console.log('Server running on portt:', port); // eslint-disable-line
});

export default app;

Now when i run following command: knex migrate:make create_employee_and_company_tables It gives folllowing error

Error: knex: Required configuration option 'client' is missing.
    at new Client (/Users/sujin.v2px/NodeJS/nodees6/node_modules/knex/lib/client.js:99:11)
    at Knex (/Users/sujin.v2px/NodeJS/nodees6/node_modules/knex/lib/index.js:56:34)
    at initKnex (/usr/local/lib/node_modules/knex/bin/cli.js:73:10)
    at Command.<anonymous> (/usr/local/lib/node_modules/knex/bin/cli.js:139:22)
    at Command.listener (/usr/local/lib/node_modules/knex/node_modules/commander/index.js:315:8)
    at emitTwo (events.js:126:13)
    at Command.emit (events.js:214:7)
   ...

Am I missing some configurations? What does the client missing actually refer to?

Sujin Shrestha
  • 1,203
  • 2
  • 13
  • 23

9 Answers9

20

This is an answer that may be helpful for some people that land here, because of the same issue where they are using typescript. (beyond the point of dotEnv issue (check the other answer for that)).

'client' is missing error and Typescript

The problem is that your typescript export default is not supported by knex cli by default.

To illustrate:

This doesn't work throwing the error above: enter image description here

And this work: enter image description here

As you can see, you can use typescript normally, even the import syntax and all. Then when you export you need to use the commonjs syntax directly.

If not appreciated, you can check this github issue for solution:

https://github.com/tgriesser/knex/issues/1232

I don't know how knex resolve for tsconfig.json. It may matter. And you may add a new tsconfig.json where knexfile.ts is located.

In my case i had that in my config (it was in my project root and not where knexfile.ts [for the project compilation])

  "compilerOptions": {
    /* Basic Options */
    // "incremental": true,                   /* Enable incremental compilation */
    "target": "ES2018",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    // "lib": [],                             /* Specify library files to be included in the compilation. */
    "allowJs": true,     

you may like to change the target.

Another important point, you must have node-ts installed, as it is used under the hood. However if you don't you may have another complete error. And don't forget to install your clients ǹpm i --save pg sqlite3 node-ts typescript knex. (you may like to separate dev dependencies).

I will update after more investigation. To explain deeply the why!

Mohamed Allal
  • 17,920
  • 5
  • 94
  • 97
11

In order to use environment variables from your .env file, pass a path argument to config like this:

require('dotenv').config({path: 'path-to-.env'})

https://github.com/tgriesser/knex/issues/590

matsad
  • 318
  • 7
  • 12
7

What solved this problem for me was in my Knexfile I was using a non-standard environment name:

let dbConnection = {
  client : "pg",
  connection: connectionObject,
  migrations: {
    directory: './db/migrations'
  },
  useNullAsDefault: true
};

module.exports = {
  connection: dbConnection
};

So I had to run knex migrate:make --env connection migration_name and it worked as expected.

bzupnick
  • 2,646
  • 4
  • 25
  • 34
  • this is what actually helped me while running `knex migrate:latest`. Using a knexfile.js which has ```module.exports = { local: { ...}, stage: { ...} }``` – cristian May 10 '22 at 10:22
4

Just another possibility here since I didn't see anyone mention it yet:

If you are also using knexfile and you are sure your client is set properly, e.g. "pg". Then make sure your environment variable matches with the knexfile.

What I mean is that, run echo $NODE_ENV to see what your NODE_ENV is.

In my case, mine is actually dev not development(default in knexfile).

Note: If NODE_ENV is empty, use NODE_ENV=development <your knex command> Example: NODE_ENV=development knex migrate:latest

Sidney
  • 4,495
  • 2
  • 18
  • 30
Elfi Yang
  • 133
  • 7
  • This was very helpful. Confirmed, there was a bug in my code where NODE_ENV was not set correctly. I have no idea why knex throws this error in response to a mismatch with NODE_ENV. Very misleading. – Grant Curell Mar 21 '23 at 13:40
4

I advise placing the client immediately below module.exports >>>

  module.exports = {
    client: 'postgresql',
    connection: {
      database:'nomedobanco',
      user:'user',
      password:'senha'
}
Aviz
  • 41
  • 2
3

You have mentioned require('dotenv').config();

require('dotenv').config();
module.exports = {
  development: {
    client: process.env.DB_CLIENT,
    connection: {
      host: process.env.DB_HOST,
      user: process.env.DB_USER,
      password: process.env.DB_PASSWORD,
      database: process.env.DB_NAME
    },
    migrations: {
      directory: __dirname + '/db/migrations'
    },
    seeds: {
      directory: __dirname + '/db/seeds'
    }
  }
};

Please make sure you have .env file in the root folder which contains your environmental variables.

# Application
APP_PORT=3000
APP_HOST=127.0.0.1

# Environment
NODE_ENV = development

# Database
DB_CLIENT=mysql
DB_HOST=localhost
DB_USER=myuser
DB_PASSWORD=*******
DB_NAME=vts  
DB_PORT=3308

Once you have ".env" file in the root folder, you will see your this error would be gone. Also make sure you have mentioned a correct DB_CLIENT in your .env file.

TAB
  • 1,944
  • 8
  • 28
  • 45
0

Your process.env.DB_CLIENT is undefined. You can verify it by hardcoding

client: 'pg',

without trying to use environment variables / dotenv.

In case where all configuration reading was failed and configuration would have been undefined, different error would have been thrown (cannot read client of undefined).

Mikael Lepistö
  • 18,909
  • 3
  • 68
  • 70
0

I Observed knexfile.js does not support env config without path.
So use as below :

 require('dotenv').config({path: './'});
Sujay U N
  • 4,974
  • 11
  • 52
  • 88
0

The simple answer of this error is you are missing client. You just need to define all three environment development staging and production


module.exports = {

  development: {
    client: "mysql",
    connection: {
      user: process.env.DB_USER,
      host: process.env.DB_HOST,
      password:process.env.DB_PASSWORD,
      database: process.env.DB_NAME
    },
    migrations: {
      directory: __dirname + '/db/migrations',
    },
    seeds: {
      directory: __dirname + '/db/seeds'
    }
  },

  staging: {
    client: "mysql",
    connection: {
      user: process.env.DB_USER,
      host: process.env.DB_HOST,
      password:process.env.DB_PASSWORD,
      database: process.env.DB_NAME
    },
    migrations: {
      directory: __dirname + '/db/migrations',
    },
    seeds: {
      directory: __dirname + '/db/seeds'
    }
  },

  production: {
    client: "mysql",
    connection: {
      user: process.env.DB_USER,
      host: process.env.DB_HOST,
      password:process.env.DB_PASSWORD,
      database: process.env.DB_NAME
    },
    migrations: {
      directory: __dirname + '/db/migrations',
    },
    seeds: {
      directory: __dirname + '/db/seeds'
    }
  }

};

aadilraza339
  • 103
  • 3