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
24k stars 604 forks source link

[BUG]: Cannot Resolve Imports with Explicit File Extensions #849

Closed zacowan closed 4 months ago

zacowan commented 1 year ago

What version of drizzle-orm are you using?

0.27.0

What version of drizzle-kit are you using?

0.19.3

Describe the Bug

drizzle-kit generate:sqlite --schema <schema> cannot resolve a table import from a file using explicit file extensions.

Steps to Reproduce

Steps

  1. Assume there is a sqlite file with the users table already created via a migration
  2. Run drizzle-kit generate:sqlite --schema ./src/picture.ts
  3. Observe the following error:
Error: Cannot find module './user.js'
Require stack:
- /Users/zacowan/Development/playgrounds/drizzle/src/picture.ts
- /Users/zacowan/Development/playgrounds/drizzle/node_modules/.pnpm/drizzle-kit@0.19.3/node_modules/drizzle-kit/index.cjs
    at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
    at Module._load (node:internal/modules/cjs/loader:920:27)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> (/Users/zacowan/Development/playgrounds/drizzle/src/picture.ts:3:23)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._compile (/Users/zacowan/Development/playgrounds/drizzle/node_modules/.pnpm/drizzle-kit@0.19.3/node_modules/drizzle-kit/index.cjs:8502:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Object.newLoader [as .ts] (/Users/zacowan/Development/playgrounds/drizzle/node_modules/.pnpm/drizzle-kit@0.19.3/node_modules/drizzle-kit/index.cjs:8506:13)
    at Module.load (node:internal/modules/cjs/loader:1117:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/zacowan/Development/playgrounds/drizzle/src/picture.ts',
    '/Users/zacowan/Development/playgrounds/drizzle/node_modules/.pnpm/drizzle-kit@0.19.3/node_modules/drizzle-kit/index.cjs'
  ]
}

Schema

// picture.ts
import { InferModel, relations } from "drizzle-orm";
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
import { users } from "./user.js";

export const pictures = sqliteTable("pictures", {
  id: integer("id").primaryKey(),
  path: text("path"),
  userId: integer("user_id").notNull(),
});
// ...
// user.ts
import { InferModel } from "drizzle-orm";
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";

export const users = sqliteTable("users", {
  id: integer("id").primaryKey(),
  name: text("name"),
});
// ...

TSConfig

With "type": "module" set in package.json.

{
  "compilerOptions": {
    "target": "ESNext",
    "moduleDetection": "auto",
    "moduleResolution": "NodeNext",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}

Expected behavior

drizzle-kit should resolve the import. When removing the .js, the command does not fail (however, TypeScript will then fail to compile the project).

Environment & setup

No response

giacomorebonato commented 1 year ago

Should this be moved to https://github.com/drizzle-team/drizzle-kit-mirror/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc ?

I can't find drizzle-kit source code, is it closed source?

zacowan commented 1 year ago

Should this be moved to https://github.com/drizzle-team/drizzle-kit-mirror/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc ?

I can't find drizzle-kit source code, is it closed source?

In hindsight, yeah looks like it. I imagine it's closed source, otherwise that mirror should have the source code. Looks like it's been labelled and assigned here, so I'll hold off on migrating it over 😄

md-redwan-hossain commented 1 year ago

Facing the same issue. It is driving me mad, if drizzle migration works, Typescript fails and vice-versa. This should be fixed with higher priority since Drizzle is labelled as Typescript ORM.

giacomorebonato commented 11 months ago

I have a workaround for this.

  1. install tsx
  2. rewrite the command like this "db:generate": "NODE_OPTIONS='--import tsx' drizzle-kit generate:sqlite",
StepanMynarik commented 11 months ago

Same problem, will try the @giacomorebonato workaround

MickL commented 6 months ago

Having the same issue, Drizzle is not usable with AdonisJS

Nearly 1 year later, still nothing? :(

dtesting1330 commented 6 months ago

The fix recommended by @giacomorebonato isn't working for me.

My package.json scripts

"scripts": {
       ...rest of the scripts, 
    "db:generate": "NODE_OPTIONS='--import tsx' drizzle-kit generate:pg",
    "db:migrate": "tsx src/db/migrator.ts",
    "db:studio": "NODE_OPTIONS='--import tsx' drizzle-kit studio --host localhost --port 8100 --verbose"
},

Error

image
ElGatoLoco commented 6 months ago

I thought those negative testimonials on your landing page were a joke, but it seems that you actually don't give a flying fuck about maintaining this. Unusable with bun + typescript + sqlite.

AndriiSherman commented 4 months ago

I just tried it with the latest drizzle-kit, which is drizzle-kit@0.22.4, and it worked well. If you still encounter this issue, feel free to open a new one with a full reproduction example

benzittlau commented 2 months ago

I'm still seeing this issue with drizzle-kit@0.24.1:

➜  flight-director git:(main) ✗ yarn run drizzle-kit generate --config drizzle.config.ts
yarn run v1.22.22
$ /Users/benzittlau/code/parakeeto-core/node_modules/.bin/drizzle-kit generate --config drizzle.config.ts
Reading config file '/Users/benzittlau/code/parakeeto-core/apps/flight-director/drizzle.config.ts'
Error: Cannot find module './src/typings/dateTypes.js'