I'm trying to use Knex.js with my Next.js application, since Knex.js is the only SQL query builder I know very well and feel most comfortable to develop with. However, I can't find a way to integrate Knex.js with my application. Here's the source code:
import knex from 'knex';
const connection = knex({
client: 'mysql',
connection: process.env.DATABASE_URL,
asyncStackTraces: true,
debug: true
});
export default connection;
And whenever I try to use it in my index page, which looks like this:
import Head from 'next/head';
import Container from 'react-bootstrap/Container';
import Col from 'react-bootstrap/Col';
import Row from 'react-bootstrap/Row';
import Navbar from '../components/Navbar';
import con from '../database/connection';
export async function getServerSideProperties(context) {
const result = await con('test').select();
return {
props: {
test: result
}
}
}
export default function Home({ test }) {
return (
<>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</Head>
<Navbar />
<Container className="text-center">
<Row>
<Col>
{test}
</Col>
</Row>
</Container>
</>
)
}
I receive this error:
error - ./node_modules/knex/lib/migrations/util/fs.js:1:0
Module not found: Can't resolve 'fs'
Did you mean './fs'?
Requests that should resolve in the current directory need to start with './'.
Requests that start with a name are treated as module requests and resolve within module directories (node_modules).
If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.
Import trace for requested module:
./node_modules/knex/lib/migrations/seed/Seeder.js
./node_modules/knex/lib/knex-builder/make-knex.js
./node_modules/knex/lib/knex-builder/Knex.js
./node_modules/knex/lib/index.js
./node_modules/knex/knex.js
./src/database/connection.js
./src/pages/index.js
https://nextjs.org/docs/messages/module-not-found
Could not find files for / in .next/build-manifest.json
Could not find files for / in .next/build-manifest.json
Is it possible to integrate Knex.js to Next.js and if it's not, then what are the alternatives?