Issue
I'm currently trying to migrate some code from javascript to typescript. This code uses the knex package. I am able to build and run it using tsc
, but i would like to use esbuild
instead (for faster build).
I build with:
npx esbuild index.ts --bundle --platform=node --sourcemap=external --outfile=dist/index.js
But when i try to run the compiled code, i get:
Cannot find module '../../dialects/mysql/index.js'
I have no idea what is this dialect
directory, and couldn't find any documentatio about it. I took a look at this SO question, but couldn't really understand the code in it, and the error seemed to be more about browserified
.
Here is the code I used to reproduce the error:
import Knex from 'knex';
const knex = Knex({
client: 'mysql',
connection: {
port: 3306,
host: 'localhost',
database: 'database',
user: 'user',
password: 'password',
charset: 'utf8mb4',
},
});
const fetchData = async () => {
const data = await knex('table')
.select()
.first();
return data;
};
(async () => {
const results = await fetchData();
console.log(results);
})();
Interesting thing is that by using only the mysql
package without knex
worked great with both tsc
and esbuild
.
For some reason only the build from esbuild
fails. I first thought i missed a package, and i installed @types/node and @types/mysql but it didn't solved my problem.
If it helps, i use the following tsconfig.json
:
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"outDir": "dist",
"target": "es6",
"esModuleInterop": true
}
}
Solution
This is a problem I've had as well and I came across a related thread on the esbuild GitHub.
You'll just need to update your knex initialisation to the following:
import Knex from 'knex';
import KnexMysql from 'knex/lib/dialects/mysql';
const knex = Knex({
client: KnexMysql,
...
});
Answered By - j-petty
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.