Suj*_*tha 7 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.env.DB_PASSWORD,
database: process.env.DB_NAME
},
migrations: {
directory: __dirname + '/db/migrations'
},
seeds: {
directory: __dirname + '/db/seeds'
}
}
};
Run Code Online (Sandbox Code Playgroud)
knex.js
const environment = process.env.NODE_ENV || 'development';
let config = require('../knexfile')[environment];
module.exports = require('knex')(config);
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
现在,当我运行以下命令时:
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)
...
Run Code Online (Sandbox Code Playgroud)
我是否缺少某些配置?什么是client缺少的其实是指什么?
Moh*_*lal 16
这是一个答案,可能对一些来到这里的人有所帮助,因为他们使用打字稿时遇到了同样的问题。(超出 dotEnv 问题的范围(请查看其他答案))。
问题是export defaultknex cli 默认不支持您的打字稿。
为了显示:
如您所见,您可以正常使用打字稿,甚至导入语法等等。那么导出的时候需要直接使用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,
Run Code Online (Sandbox Code Playgroud)
你可能想改变目标。
另一个重要的点,您必须已node-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
};
Run Code Online (Sandbox Code Playgroud)
所以我必须运行knex migrate:make --env connection migration_name并且它按预期工作。
为了使用.env文件中的环境变量,请传递一个路径参数,config如下所示:
require('dotenv').config({path: 'path-to-.env'})
Run Code Online (Sandbox Code Playgroud)
https://github.com/tgriesser/knex/issues/590
你的process.env.DB_CLIENT是undefined。您可以通过硬编码来验证它
client: 'pg',
Run Code Online (Sandbox Code Playgroud)
无需尝试使用环境变量/dotenv。
如果所有配置读取失败并且配置未定义,则会抛出不同的错误(无法读取client)undefined。