drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.03k stars 608 forks source link

[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

Open deadcoder0904 opened 1 year ago

deadcoder0904 commented 1 year ago

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"]
}
lefuncq commented 11 months ago

Any updates on that?

Angelelz commented 11 months ago

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.

deadcoder0904 commented 11 months ago

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

matheuspuel commented 11 months ago

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"

Angelelz commented 11 months ago

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.

deadcoder0904 commented 11 months ago

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.

Angelelz commented 11 months ago

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?

binochoi commented 10 months ago

To make it easier, we need to actively utilize type import and place important codes such as validation near schema. It may be quite a jarring arrangement if you use nosql or the others that cant reached drizzle.

deadcoder0904 commented 8 months ago

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

szcrow commented 4 months ago

this also happens with sveltekit's $lib alias,

Error: Cannot find module '$lib/utils/mymodule'

L-Mario564 commented 1 week ago

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.