Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Knex所需配置选项';客户';是丢失的错误_Javascript_Node.js_Knex.js - Fatal编程技术网

Javascript Knex所需配置选项';客户';是丢失的错误

Javascript Knex所需配置选项';客户';是丢失的错误,javascript,node.js,knex.js,Javascript,Node.js,Knex.js,这是我的档案。 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

这是我的档案。 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迁移:创建员工表和公司表
它给出了如下错误

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)
   ...
错误:knex:缺少必需的配置选项“客户端”。
在新客户端(/Users/sujin.v2px/NodeJS/nodees6/node_modules/knex/lib/Client.js:99:11)
在Knex(/Users/sujin.v2px/NodeJS/nodees6/node_modules/Knex/lib/index.js:56:34)
在initKnex(/usr/local/lib/node_modules/knex/bin/cli.js:73:10)
指挥。(/usr/local/lib/node_modules/knex/bin/cli.js:139:22)
在Command.listener(/usr/local/lib/node_modules/knex/node_modules/commander/index.js:315:8)
两点钟(events.js:126:13)
at Command.emit(events.js:214:7)
...

我是否缺少一些配置?
客户端
缺失实际上指的是什么?

您的
进程。env.DB\u客户端
未定义的
。您可以通过硬编码进行验证

client: 'pg',
不尝试使用环境变量/dotenv


如果所有配置读取都失败并且配置未定义,则会引发不同的错误(无法读取
未定义的
客户端

要使用
.env
文件中的环境变量,请向
配置
传递路径参数,如下所示:

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

我发现knexfile.js不支持没有路径的环境配置。
因此,请按以下方式使用:

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

这是一个可能对一些登陆这里的人有用的答案,因为他们在使用typescript时遇到了同样的问题。(超出dotEnv问题的范围(请检查其他答案)

“客户端”缺少错误和类型脚本 问题是默认情况下knex cli不支持您的typescript
导出默认值

举例说明:

如果抛出上述错误,则此操作无效:

这项工作:

如您所见,您可以正常使用typescript,甚至包括导入语法和所有。然后在导出时,需要直接使用commonjs语法

如果您不喜欢,您可以查看此github问题以获得解决方案:

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

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

您可能想更改目标

另一个要点是,必须安装
节点ts
,因为它是在发动机罩下使用的。但是,如果不这样做,则可能会出现另一个完全错误。别忘了安装客户端
ǹpm i--save pg sqlite3 node ts typescript knex
。(您可能希望分离开发依赖项)


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

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

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

module.exports = {
  connection: dbConnection
};

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

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

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

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


在我的例子中,我的实际上是
dev
而不是
development
(默认为
knexfile
)。

@Vassi Glade听到这一点很高兴。您的语句末尾缺少一个花括号自我提醒:当.env文件中缺少配置时会发生错误。
let dbConnection = {
  client : "pg",
  connection: connectionObject,
  migrations: {
    directory: './db/migrations'
  },
  useNullAsDefault: true
};

module.exports = {
  connection: dbConnection
};