kysely-ctl
kysely-ctl copied to clipboard
Importing file with alias path is not working
Description
Currently, the kysely.config.ts file is importing the kysely instance I created on a separate file and the path is an alias path like @/path/to/kysely.
This throws an error on the CLI.
Looking at c12, it looks like it's exposing jitiOptions which also exposes the alias to allow alias path.
ERROR Cannot find module '@/path/to/kysely'
Require stack:
- /repo/kysely.config.ts
Require stack:
- kysely.config.ts
at Module._resolveFilename (node:internal/modules/cjs/loader:1143:15)
at Function.resolve (node:internal/modules/helpers:190:19)
at _resolve (node_modules/jiti/dist/jiti.js:1:241814)
at jiti (node_modules/jiti/dist/jiti.js:1:244531)
at kysely.config.ts:2:15
at evalModule (node_modules/jiti/dist/jiti.js:1:247313)
at Object.jiti (node_modules/jiti/dist/jiti.js:1:245241)
at resolveConfig (node_modules/c12/dist/shared/c12.cab0c9da.mjs:345:26)
at loadConfig (node_modules/c12/dist/shared/c12.cab0c9da.mjs:147:29)
at async getConfig (node_modules/kysely-ctl/dist/bin.js:226:24)
Workaround
Manually configure using dialect and dialectConfig essentially repeating the same logic.
Package Info
kysely v0.27.4
kysely-ctl v0.9.0
Another workaround is to use jiti inside the config and move .env.local to .env for Next.js application
import { fileURLToPath } from 'url';
import createJiti from 'jiti';
import { defineConfig } from 'kysely-ctl';
const moduleFileUrl = import.meta.url;
const jiti = createJiti(fileURLToPath(moduleFileUrl), {
// Since `jiti` uses isolated environment, we need to provide an alias for the src directory
// See: https://github.com/unjs/jiti/issues/104
alias: { '@': fileURLToPath(new URL('./src', moduleFileUrl)) },
});
const kysely = jiti('./src/path/to/kysely.ts');
export default defineConfig({
kysely,
// other kysely-ctl config
});
I encountered the same issue. Luckily @JeromeDeLeon's workaround works!
Hey @JeromeDeLeon 👋
Thanks for raising this and coming up with a workaround! 💪
jiti does not automatically infer aliases from tsconfig or other sources by design.
Where are you configuring your aliases?
We're relying on tsconfig for this one.
Another workaround is to use
jitiinside the config and move.env.localto.envforNext.jsapplicationimport { fileURLToPath } from 'url'; import createJiti from 'jiti'; import { defineConfig } from 'kysely-ctl'; const moduleFileUrl = import.meta.url; const jiti = createJiti(fileURLToPath(moduleFileUrl), { // Since `jiti` uses isolated environment, we need to provide an alias for the src directory // See: https://github.com/unjs/jiti/issues/104 alias: { '@': fileURLToPath(new URL('./src', moduleFileUrl)) }, }); const kysely = jiti('./src/path/to/kysely.ts'); export default defineConfig({ kysely, // other kysely-ctl config });
Do you know why we have to move .env.local to .env even if we do
import { loadEnvConfig } from '@next/env';
loadEnvConfig(process.cwd());
regardless?
Check https://github.com/kysely-org/kysely-ctl/releases/tag/v0.11.0. Lemme know if there are any issues.
@igalklebanov thanks for this - doesn't seem to work with sveltekit ?
my tsconfig is pretty vanilla
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"moduleResolution": "bundler",
"types": ["node"]
}
// Path aliases are handled by https://svelte.dev/docs/kit/configuration#alias
// except $lib which is handled by https://svelte.dev/docs/kit/configuration#files
//
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
// from the referenced tsconfig.json - TypeScript does not merge them in
}
ERROR Cannot find module '$lib/server/utils/envVars'
Require stack:
- /workspaces/sveltekitproject/src/lib/server/db/seeds/1751828495172_my_seed.ts
at Function._resolveFilename (node:internal/modules/cjs/loader:1365:15)
at Function.resolve (node:internal/modules/helpers:145:19)
at jitiResolve (node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.cjs:1:187220)
at jitiRequire (node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.cjs:1:189288)
at import (node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.cjs:1:199778)
at src/lib/server/db/seeds/1751828495172_my_seed.ts:2:22
at eval_evalModule (node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.cjs:1:196325)
at jitiRequire (node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.cjs:1:190233)
at Function.import (node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.cjs:1:199778)
at importTSFile (node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/kysely-ctl/dist/chunk-HQU22GLO.js:131:27)
at async FileSeedProvider.getSeeds (node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/kysely-ctl/dist/chunk-HQU22GLO.js:235:26)
at async Seeder.getSeeds (node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/kysely-ctl/dist/chunk-HQU22GLO.js:264:19)
at async Seeder.run (node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/kysely-ctl/dist/chunk-HQU22GLO.js:271:19)
at async node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/kysely-ctl/dist/bin.js:948:12
at async usingKysely (node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/kysely-ctl/dist/bin.js:581:12)
at async usingSeeder (node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/kysely-ctl/dist/bin.js:946:10)
at async Object.run (node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/kysely-ctl/dist/bin.js:1002:23)
at async runCommand (node_modules/.pnpm/[email protected]/node_modules/citty/dist/index.mjs:316:16)
at async runCommand (node_modules/.pnpm/[email protected]/node_modules/citty/dist/index.mjs:307:11)
at async runMain (node_modules/.pnpm/[email protected]/node_modules/citty/dist/index.mjs:445:7)
at async Object.parse (node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/kysely-ctl/dist/bin.js:1273:7)
ERROR Cannot find module '$lib/server/utils/envVars'
Require stack:
- /workspaces/sveltekitproject/src/lib/server/db/seeds/1751828495172_my_seed.ts
As a workaround I am using
tsx -r tsconfig-paths/register ./node_modules/kysely-ctl/dist/bin.js seed run
"kysely-ctl": "^0.14.0",
"kysely": "^0.28.4",
I don't use every framework on the planet. First time I hear of this.
The current solution finds the tsconfig.json file and processes its paths prop if exists. This is good enough for most cases.
Svelte seems to push you to do something else, so this demands extra processing.
I am relatively new to the modern js ecosystem, I am really enjoying using it. It doesn’t seem to “get in the way” like the big players react flavours/vue etc.. It was all very overwhelming for me.
This is why I started to use Kysely - it just does what you ask it to with zero ceremony. Really fantastic. Thank you!
No problem! 😀
BTW, try https://github.com/kysely-org/kysely-ctl/releases/tag/v0.15.0. I added a test that mimics the Svelte setup in CI to verify.
Hmm same issue. N.B. This is when importing (and calling) a function from an aliased path. Importing types works correctly though. I don't know enough about this really.
"kysely-ctl": "^0.15.1",
"kysely": "^0.28.4",
https://stackblitz.com/edit/sveltejs-kit-template-default-rpqfn5qp?file=src%2Fkysely.config.ts
If you create a new terminal cd src && kysely migrate list (or any other command that tries to load the config) it will exhibit the behaviour above
this doesn't want to resolve aliases from the root directory, so I put kysely.config.ts under src (This will be a sveltekit configuration that I am unsure how to fix)
You need to use the --experimental-resolve-tsconfig-paths flag.
kysely.config should be placed where the c12 package expects to find it. Root or .config folder iirc. Add it's path to your tsconfig.json's include list if you're getting compilation-errors for paths.
This is amazing, thanks!
SvelteKit has this configuration option https://svelte.dev/docs/kit/configuration#typescript So you can do
typescript: {
config: (config) => {
config.include = [...(config.include || []), '../kysely.config.ts'];
return config;
}
}
Then regenerate the sveltekit, and the file gets processed correctly.
I notice that --experimental-resolve-tsconfig-paths has to be at the end of the command (contrary to the help docs, USAGE kysely seed [OPTIONS] list|make|run), which is fine, it just means I can't have a setup like this in my package.json
"db": "kysely --experimental-resolve-tsconfig-paths",
"db:migrate": "pnpm db migrate",
"db:seed": "pnpm db seed --experimental-resolve-tsconfig-paths", // or this
I worked around it by creating a custom script
#!/bin/sh
pnpm kysely "$@" --experimental-resolve-tsconfig-paths
Then it always sticks the flag always at the end
So good, thanks so much. I shall change this if it goes out of experimental 😄
I might be brave enough soon and remove the experimental flag. 😅