Repository navigation
[BUG]: drizzle.config.ts doesn't follow tsconfig.json for module path aliases (@/app/lib/env) but works for absolute paths (./src/app/lib/env) #1228
Description
Activity
- changed the title
[-][BUG]: Drizzle Config doesn't follow Tsconfig[/-][+][BUG]: `drizzle.config.ts` doesn't follow `tsconfig.json` for module path aliases (`@/app/lib/env`) but works for absolute paths (`./src/app/lib/env`)[/+]on Sep 16, 2023 Any updates on that?
Reacted by a2kdrizzle.config.tsdoesn't have a way to follow your path aliases because it doesn't know about them. It depends on how you are running the script. The workaround will depend on if you're running it withts-node,tsc,tsxor any other. You probably just need to point the script runner/transpiler to yourtsconfig.json.Reacted by a2k and Sacha Herszkorn@Angelelz how do i do that then?
i already have this in my
package.jsonscripts:"db:push": "drizzle-kit push:mysql --config drizzle.config.ts", "db:generate": "drizzle-kit generate:mysql --config drizzle.config.ts", "db:studio": "drizzle-kit studio --host localhost --port 3001 --verbose --config drizzle.config.ts",
all i do is
pnpm run db:pushso idk howtscand the likes come into play.This is how I'm doing:
"db:generate": "ts-node -r tsconfig-paths/register --project ./tsconfig.json ./node_modules/drizzle-kit/index.cjs generate:pg --config ./drizzle.config.ts"Reacted by a2k and Bhattarapong SomwongReacted by robinochThis is how I'm doing:
"db:generate": "ts-node -r tsconfig-paths/register --project ./tsconfig.json ./node_modules/drizzle-kit/index.cjs generate:pg --config ./drizzle.config.ts"I honestly just used relative paths and called it a day, lol. Drizzle kit just need the schema and the migration folder. I felt like an alias for those wasn't necessary. So there you go.
Reacted by a2kyou're right @Angelelz!
thank you @matheuspuel for the solution but i'll stick with relative paths.
i always followed 1 right way of doing things for 10 years now. i guess its time to drop bad habits with whatever works. not gonna install
ts-nodeha.still keeping this one open if drizzle team finds a solution.
Reacted by Matheus Michalski Puel, Tautvydas Steponavičius and Adamstill keeping this one open if drizzle team finds a solution.
I agree with this. Maybe they can include an option to point to the
tsconfig.json?Reacted by cellulosa, Doug and Klein PetrThis works for me now, at least, in combination with
@t3-oss/env-nextjsSee https://github.com/deadcoder0904/drizzle-t3-oss-env/blob/main/drizzle.config.ts#L5
- addeddocsImprovements or additions to documentationImprovements or additions to documentationand removedbugSomething isn't workingSomething isn't working
on Oct 16, 2024 This is more of a TS issue than a Drizzle one. We'll keep track of this as I do think this should be mentioned in the docs.
Reacted by alexIn my project I'm using tsx, so I was able to set it up like this:
"db:generate": "tsx node_modules/.bin/drizzle-kit generate --config=src/lib/db/config.ts --name=migration",
The .bin/drizzle-kit symlink points to drizzle-kit/bin.cjs.
Reacted by Sajith AReacted by Sarah@AndriiSherman completed by what?
Reacted by Wind, Sandro Circi, Doug, Michael Schaufelberger, a2k and Samuel Mortensonyeah I don't understand it either, it still doesn't read tsconfig
might not being released yet tho. Last release was dec 13th, this was closed on dec 16th.
But yeah it could be useful to know which PR/commit fixed this
This is how I'm doing:
"db:generate": "ts-node -r tsconfig-paths/register --project ./tsconfig.json ./node_modules/drizzle-kit/index.cjs generate:pg --config ./drizzle.config.ts"I honestly just used relative paths and called it a day, lol. Drizzle kit just need the schema and the migration folder. I felt like an alias for those wasn't necessary. So there you go.
In my case, we have schema files split, and they all have their own imports for types, for example. Those are also in other parts of the project. So, having that part of the codebase use relative paths feels very wrong and like a workaround. Please reconsider re-opening this issue. I'm also happy to file a new one if you need more context.
Reacted by Oliver PengYou can fix by adding tsx as a devDependency and setting your db:migrate, db:push, etc. to "tsx node_modules/drizzle-kit/bin.cjs [cmd]"
Reacted by Oliver Peng and NavaruYou can fix by adding tsx as a devDependency and setting your db:migrate, db:push, etc. to "tsx node_modules/drizzle-kit/bin.cjs [cmd]"
Confirming that this worked as a solution for me. I was already using tsx to run seed & migration scripts, so it wasn't even an extra dependency.
Reacted by alexI had to combine a few things to make it work for me:
"scripts": { "db:generate": "dotenv -e .env.local -- tsx --tsconfig ./packages/db/tsconfig.json node_modules/drizzle-kit/bin.cjs generate --config=packages/db/drizzle.config.ts", }
dotenv -e .env.local # I use `dotenv` and `dotenv-cli` to get environment variables working -- # This is needed otherwise dotenv won't work tsx # Need to use tsx to get module resolution --tsconfig ./packages/db/tsconfig.json # This is needed to specify which tsconfig.json file to target. # Otherwise it didn't work for me without this explicit specification node_modules/drizzle-kit/bin.cjs # Same as `node_module/.bin/drizzle-kit/` to trigger drizzle-kit using tsx generate # drizzle-kit's generate function --config=packages/db/drizzle.config.ts # where my drizzle.config file was
Long and ugly but it works. Main addition being specifying where the tsx file is with
--tsconfigin addition to what was mentioned aboveEdit: You can also just add a
tsconfig.jsonfile in the root of your project and remove the--tsconfig <path>partReacted by Navaru, Agapios Panos and Sajith AReacted by Agapios Panos@AndriiSherman why closed?
usingtsxyou can do something like this in yourpackage.json"db:drizzle": "tsx node_modules/drizzle-kit/bin.cjs --config ./drizzle.config.ts", "db:generate": "pnpm db:drizzle generate", "db:migrate": "pnpm db:drizzle migrate"
Reacted by Fritz Gerlin Senat, Diego Díaz, Luciano Longo, Pas and Alexandre Le Borgnethis also happens with sveltekit's $lib alias,
Error: Cannot find module '$lib/utils/mymodule'If you are having issues with SvelteKit and you are using Bun, you can just
"scripts": { "db:generate": "bunx --bun drizzle-kit generate", }
What version of
drizzle-ormare you using?^0.28.6
What version of
drizzle-kitare you using?^0.19.13
Describe the Bug
if i import using
'@/app/lib/env', it doesn't work but if i import using'./src/app/lib/env', it works.for example, this works fine:
drizzle.config.ts
but the following throws an error
Error: Cannot find module '@/app/lib/env':drizzle.config.ts
my
envfile looks like this:lib/env.ts
Expected behavior
this should work.
drizzle.config.ts
Environment & setup
windows 10!
my tsconfig looks like:
tsconfig.json
{ "compilerOptions": { "target": "ESNext", "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true, "strict": true, "forceConsistentCasingInFileNames": true, "noEmit": true, "esModuleInterop": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "jsx": "preserve", "incremental": true, "plugins": [ { "name": "next" } ], "paths": { "@/*": ["./src/*"] } }, "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], "exclude": ["node_modules"] }