【问题标题】:Knex required configuration option 'client' is missing errorKnex 所需的配置选项“客户端”丢失错误
【发布时间】:2018-08-30 09:29:34
【问题描述】:

这是我的文件。 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;

现在当我运行以下命令时: knex migrate:make create_employee_and_company_tables 它给出了以下错误

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)
   ...

我是否缺少一些配置? client 失踪实际上指的是什么?

【问题讨论】:

  • 自我提醒:.env文件中缺少配置时发生错误。

标签: javascript node.js knex.js


【解决方案1】:

这个答案可能对在这里登陆的一些人有所帮助,因为他们使用打字稿的问题相同。 (超出 dotEnv 问题的重点(请查看其他答案)。

'client' 缺少错误和 Typescript

问题是 knex cli 默认不支持你的 typescript export default

举例说明:

这不起作用抛出上面的错误:

而这项工作:

如您所见,您可以正常使用 typescript,甚至导入语法等等。那么导出的时候就需要直接使用commonjs语法了。

如果不满意,您可以查看此 github 问题以获取解决方案:

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

我不知道 knex 如何解析 tsconfig.json。这可能很重要。您可以在 knexfile.ts 所在的位置添加一个新的 tsconfig.json。

在我的情况下,我的配置中有它(它在我的项目根目录中,而不是 knexfile.ts [用于项目编译]的位置)

  "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,     

您可能想更改目标。

另一点很重要,您必须安装node-ts,因为它在后台使用。但是,如果您不这样做,您可能会遇到另一个完整的错误。并且不要忘记安装您的客户端ǹpm i --save pg sqlite3 node-ts typescript knex。 (您可能希望分离开发依赖项)。

我会在更多调查后更新。深入解释原因!

【讨论】:

  • @Vassi Glade 听到了。
【解决方案2】:

为了使用.env 文件中的环境变量,请将路径参数传递给config,如下所示:

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

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

【讨论】:

  • 您的语句末尾缺少一个大括号
【解决方案3】:

为我解决这个问题的原因是在我的 Knexfile 中我使用了一个非标准的环境名称:

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

module.exports = {
  connection: dbConnection
};

所以我不得不运行knex migrate:make --env connection migration_name,它按预期工作。

【讨论】:

    【解决方案4】:

    我建议将客户端直接放在 module.exports 下方 >>>

      module.exports = {
        client: 'postgresql',
        connection: {
          database:'nomedobanco',
          user:'user',
          password:'senha'
    }
    

    【讨论】:

    • 救命评论
    【解决方案5】:

    这里只是另一种可能性,因为我还没有看到有人提到它:

    如果您也在使用 knexfile 并且您确定您的 client 设置正确,例如“pg”。 然后确保您的环境变量与knexfile 匹配。

    我的意思是,运行 echo $NODE_ENV 以查看您的 NODE_ENV 是什么。

    就我而言,我的实际上是dev 而不是development(默认为knexfile)。

    【讨论】:

      【解决方案6】:

      您的process.env.DB_CLIENTundefined。你可以通过硬编码来验证它

      client: 'pg',
      

      不尝试使用环境变量/dotenv。

      如果所有配置读取失败并且配置未定义,则会引发不同的错误(无法读取client of undefined)。

      【讨论】:

        【解决方案7】:

        我观察到 knexfile.js 不支持没有路径的环境配置。
        所以使用如下:

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

        【讨论】:

          【解决方案8】:

          你提到了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'
              }
            }
          };
          

          请确保您的根文件夹中有.env 文件,其中包含您的环境变量。

          # 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
          

          一旦你在根文件夹中有“.env”文件,你会看到你的这个错误会消失。还要确保您在 .env 文件中提到了正确的 DB_CLIENT

          【讨论】:

            猜你喜欢
            • 2018-10-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-02-07
            • 2016-08-19
            • 2013-04-26
            • 2014-10-08
            • 1970-01-01
            相关资源
            最近更新 更多