I'm trying to make a query on my local MySQL database, but when I use the knex it says for me to install the driver of sqlite3 which I don't need because my database is a MySQL.
I tried to install the drive of sqlite3 just to see if it would fix my problem but it just made other errors related to sqlite3 show up, so I just removed the sqlite3 driver.
I already have the driver of MySQL. Below is my code.
Why this error is happening? From what I know it shouldn't be asking me for a sqlite3 driver but just the MySQL as I passed in the configuration for knex.
*** I had already tried to use the driver mysql2 and nothing changed...
const knex = require('knex');
require('dotenv').config();
const listUsers = async (req, res) => {
console.log(process.env.DATABASE_PASSWORD);
//res.send(process.env.DATABASE_USER);
knex({
client: 'mysql',
connection: {
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME
}
})
knex('users')
.select('*')
.then(mysqlres => res.send(mysqlres))
.catch(mysqlerr => res.send(console.log(mysqlerr)))
}
module.exports = {
listUsers
};
and this is my error :
Knex: run
$ npm install sqlite3 --save
Cannot find module 'sqlite3'
Require stack:
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\dialects\sqlite3\index.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\knex.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\index.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\knex.js
- D:\coding\Studies\Moblex\backend-mysql\src\controllers\userController.js
- D:\coding\Studies\Moblex\backend-mysql\src\routes.js
- D:\coding\Studies\Moblex\backend-mysql\src\server.js
Error: Cannot find module 'sqlite3'
Require stack:
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\dialects\sqlite3\index.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\knex.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\index.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\knex.js
- D:\coding\Studies\Moblex\backend-mysql\src\controllers\userController.js
- D:\coding\Studies\Moblex\backend-mysql\src\routes.js
- D:\coding\Studies\Moblex\backend-mysql\src\server.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
at Function.Module._load (internal/modules/cjs/loader.js:841:27)
at Module.require (internal/modules/cjs/loader.js:1025:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Client_SQLite3._driver (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\dialects\sqlite3\index.js:36:12)
at Client_SQLite3.initializeDriver (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\client.js:232:26)
at Client_SQLite3.Client (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\client.js:70:10)
at new Client_SQLite3 (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\dialects\sqlite3\index.js:18:10)
at new Knex (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\knex.js:53:28)
at Knex (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\knex.js:17:12)
(node:12648) UnhandledPromiseRejectionWarning: Error: Knex: run
$ npm install sqlite3 --save
Cannot find module 'sqlite3'
Require stack:
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\dialects\sqlite3\index.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\knex.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\index.js
- D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\knex.js
- D:\coding\Studies\Moblex\backend-mysql\src\controllers\userController.js
- D:\coding\Studies\Moblex\backend-mysql\src\routes.js
- D:\coding\Studies\Moblex\backend-mysql\src\server.js
at Client_SQLite3.initializeDriver (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\client.js:236:13)
at Client_SQLite3.Client (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\client.js:70:10)
at new Client_SQLite3 (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\dialects\sqlite3\index.js:18:10)
at new Knex (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\knex.js:53:28)
at Knex (D:\coding\Studies\Moblex\backend-mysql\node_modules\knex\lib\knex.js:17:12)
at listUsers (D:\coding\Studies\Moblex\backend-mysql\src\controllers\userController.js:19:5)
at Layer.handle [as handle_request] (D:\coding\Studies\Moblex\backend-mysql\node_modules\express\lib\router\layer.js:95:5)
at next (D:\coding\Studies\Moblex\backend-mysql\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\coding\Studies\Moblex\backend-mysql\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\coding\Studies\Moblex\backend-mysql\node_modules\express\lib\router\layer.js:95:5)
(node:12648) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:12648) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
So guys i just figured that i need to make a new instance of Knex when i apply the configuration and that will work ! And the reason that it was asking for the drive sqlite3 before is because knex uses the sqlite3 model. So because i was not making a new instance of the Knex with my own configuration it was using it's default.
so this is my code for the sollution !
const listUsers = async (req, res) => {
const knexapp = knex({
client: 'mysql2',
connection: {
database: process.env.DATABASE_NAME,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
})
knexapp('users').select('*').then(resmsql => res.send(resmsql)).catch(err => res.send(err))