I'm evaluating Node.js 14.x
for a new project. I'm testing Knex.js
and Objection.js
to interact with databases so I'm building a proof-of-concept project, but I'm running into some issues when trying to run the migrations (using npx knex --esm migrate:latest
):
[x08486@uplink:~/Workshop/Development/node-express]$ npx knex --esm migrate:latest
Error: knex: Required configuration option 'client' is missing.
at new Client (/home/x08486/Workshop/Development/node-express/node_modules/knex/lib/client.js:54:11)
at Knex (/home/x08486/Workshop/Development/node-express/node_modules/knex/lib/knex.js:14:28)
at initKnex (/home/x08486/Workshop/Development/node-express/node_modules/knex/bin/cli.js:56:10)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async Command.<anonymous> (/home/x08486/Workshop/Development/node-express/node_modules/knex/bin/cli.js:170:26)
I can infer that indeed the knex
client might be missing, but I don't really understand that because it's imported in knexfile.js
. I suspect this might have something to do with the fact that I'm using ES6, but not entirely sure.
This is (an excerpt of) my package.json
file:
{
"type": "module",
"scripts": {
"start": "node src/main.js"
},
"dependencies": {
"dotenv": "8.2.0",
"express": "4.17.1",
"knex": "0.21.18",
"objection": "2.2.14",
"sqlite3": "5.0.2"
},
"engines": {
"node": "14.x"
},
}
...then the knexfile.js
(at the project's root also):
import dotenv from "dotenv";
import knex from "knex";
import path from "path";
dotenv.config();
const config = {
// ...there are some more, but I'm using "sandbox" via dotenv
sandbox: {
client: "sqlite3",
connection: {
filename: "./sandbox.sqlite3", // ...or ":memory:"
},
debug: true,
migrations: {
directory: path.join(__dirname, "migrations"),
tableName: "knex_schema_history",
},
seeds: {
directory: path.join(__dirname, "seeds", "sandbox"),
},
useNullAsDefault: true,
},
};
const database = knex(config[process.env.NODE_ENV]);
export { database };
And finally the migrations/
sub-directory (at the project's root) with this content:
// 20210302134908_init.js
export function down(knex) {
return Promise.all([knex.schema.dropTable("customers")]);
}
export function up(knex) {
return Promise.all([
knex.schema.createTable("customers", function (table) {
// https://knexjs.org/#Schema-Building
table.uuid("id").notNullable().primary();
table.string("first_name", 50).notNullable();
table.string("last_name", 50).notNullable();
table.string("email", 100).index().notNullable();
// table.timestamp("created_at").defaultTo(knex.fn.now());
// table.timestamp("updated_at").defaultTo(knex.fn.now());
table.timestamps(false, true);
}),
]);
}
Any clues what's going on here?