Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knex required configuration option 'client' is missing error

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?

like image 767
Sujin Shrestha Avatar asked Aug 30 '18 09:08

Sujin Shrestha


3 Answers

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!

like image 185
Mohamed Allal Avatar answered Nov 01 '22 02:11

Mohamed Allal


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

like image 8
matsad Avatar answered Nov 01 '22 02:11

matsad


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.

like image 6
bzupnick Avatar answered Nov 01 '22 01:11

bzupnick