Skip to content

[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

@deadcoder0904

What version of drizzle-orm are you using?

^0.28.6

What version of drizzle-kit are 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

import type { Config } from 'drizzle-kit'
import * as dotenv from 'dotenv'

import { requireEnvVariable } from './src/app/lib/env'

dotenv.config({ path: '.env.local' })

const PLANETSCALE_DATABASE_URL = requireEnvVariable('PLANETSCALE_DATABASE_URL')

export default {
  schema: './src/app/db/schema.ts',
  out: './src/app/db/migrations',
  driver: 'mysql2',
  dbCredentials: {
    connectionString: PLANETSCALE_DATABASE_URL,
  },
} satisfies Config

but the following throws an error Error: Cannot find module '@/app/lib/env':

drizzle.config.ts

import type { Config } from 'drizzle-kit'
import * as dotenv from 'dotenv'

import { requireEnvVariable } from '@/app/lib/env'

dotenv.config({ path: '.env.local' })

const PLANETSCALE_DATABASE_URL = requireEnvVariable('PLANETSCALE_DATABASE_URL')

export default {
  schema: './src/app/db/schema.ts',
  out: './src/app/db/migrations',
  driver: 'mysql2',
  dbCredentials: {
    connectionString: PLANETSCALE_DATABASE_URL,
  },
} satisfies Config

my env file looks like this:

lib/env.ts

export const requireEnvVariable = (key: string) => {
  const value = process.env[key]
  if (value) {
    return value
  }

  throw new Error(`Environment variable ${key} was not defined!`)
}

Expected behavior

this should work.

drizzle.config.ts

import type { Config } from 'drizzle-kit'
import * as dotenv from 'dotenv'

import { requireEnvVariable } from '@/app/lib/env'

dotenv.config({ path: '.env.local' })

const PLANETSCALE_DATABASE_URL = requireEnvVariable('PLANETSCALE_DATABASE_URL')

export default {
  schema: './src/app/db/schema.ts',
  out: './src/app/db/migrations',
  driver: 'mysql2',
  dbCredentials: {
    connectionString: PLANETSCALE_DATABASE_URL,
  },
} satisfies Config

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"]
}

Activity

  1. 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
  2. lefuncq commented on Nov 21, 2023

    @lefuncq

    Any updates on that?

  3. Angelelz commented on Nov 28, 2023

    @Angelelz
    Contributor

    drizzle.config.ts doesn'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 with ts-node, tsc, tsx or any other. You probably just need to point the script runner/transpiler to your tsconfig.json.

  4. deadcoder0904 commented on Nov 30, 2023

    @deadcoder0904
    Author

    @Angelelz how do i do that then?

    i already have this in my package.json scripts:

    "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:push so idk how tsc and the likes come into play.

  5. matheuspuel commented on Nov 30, 2023

    @matheuspuel

    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"

  6. Angelelz commented on Nov 30, 2023

    @Angelelz
    Contributor

    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.

  7. deadcoder0904 commented on Dec 1, 2023

    @deadcoder0904
    Author

    you'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-node ha.

    still keeping this one open if drizzle team finds a solution.

  8. Angelelz commented on Dec 1, 2023

    @Angelelz
    Contributor

    still 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?

  9. deadcoder0904 commented on Feb 9, 2024

    @deadcoder0904
    Author

    This works for me now, at least, in combination with @t3-oss/env-nextjs

    See https://github.com/deadcoder0904/drizzle-t3-oss-env/blob/main/drizzle.config.ts#L5

  10. added
    docsImprovements or additions to documentation
    and removed
    bugSomething isn't working
    on Oct 16, 2024
  11. L-Mario564 commented on Oct 16, 2024

    @L-Mario564
    Contributor

    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.

  12. msikma commented on Dec 13, 2024

    @msikma

    In 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.

  13. Mardoxx commented on Dec 23, 2024

    @Mardoxx

    @AndriiSherman completed by what?

  14. productdevbook commented on Dec 23, 2024

    @productdevbook

    yeah I don't understand it either, it still doesn't read tsconfig

  15. sandros94 commented on Dec 23, 2024

    @sandros94

    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

  16. michaelschufi commented on Jan 24, 2025

    @michaelschufi

    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.

  17. MattpaulKing commented on Feb 2, 2025

    @MattpaulKing

    You 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]"

  18. Mardoxx commented on Feb 2, 2025

    @Mardoxx
  19. oliverpeng commented on Feb 18, 2025

    @oliverpeng

    You 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.

  20. paulwongx commented on Mar 7, 2025

    @paulwongx

    I 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 --tsconfig in addition to what was mentioned above

    Edit: You can also just add a tsconfig.json file in the root of your project and remove the --tsconfig <path> part

  21. rebasecase commented on Jun 17, 2025

    @rebasecase

    @AndriiSherman why closed?
    using tsx you can do something like this in your package.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"
  22. kuroski commented on Dec 29, 2025

    @kuroski

    this 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",
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions