drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

CLI generate migrations fails with ts module resolution "nodenext"

Open mandarzope opened this issue 1 year ago • 9 comments

While running migrations in following setup "drizzle-kit": "^0.22.7" throws error with typescript and type:module setup

// tsconfig.json
    "module": "nodenext",
    "moduleResolution": "nodenext",

// src/auth/schema/auth.models.ts - drizzle schema files 
import { users } from '../../core/schema/core.models.js'
// Error
Error: Cannot find module '../../core/schema/core.models.js'
Require stack:
- /Users/unknown/work/src/auth/schema/auth.models.ts
- /Users/unknown/work/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs
    at Module._resolveFilename (node:internal/modules/cjs/loader:1212:15)
    at Module._resolveFilename (/Users/unknown/work/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:19969:40)
    at Module._load (node:internal/modules/cjs/loader:1038:27)
    at wrapModuleLoad (node:internal/modules/cjs/loader:212:19)
    at Module.require (node:internal/modules/cjs/loader:1297:12)
    at require (node:internal/modules/helpers:123:16)
    at Object.<anonymous> (/Users/unknown/work/src/auth/schema/auth.models.ts:2:23)
    at Module._compile (node:internal/modules/cjs/loader:1460:14)
    at Module._compile (/Users/unknown/work/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:17466:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1544:10) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/unknown/work/src/auth/schema/auth.models.ts',
    '/Users/unknown/work/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs'
  ]
}

Currently this is fixed by modifying drizzle-kit/bin.cjs

      Module._resolveFilename = function(request, _parent) {
        const isCoreModule = _module2.builtinModules.includes(request);
        if (!isCoreModule) {
          const found = matchPath(request);
          if (found) {
            const modifiedArguments = [found, ...[].slice.call(arguments, 1)];
            return originalResolveFilename.apply(this, modifiedArguments);
          }
        }
       /***********************Temporary fix start***************************/

      if (request.endsWith('.js')) {
        const found = path.resolve(_parent.path, request.replace('.js', '.ts'))
        if (fs.existsSync(found)) {
          const modifiedArguments = [found, ...[].slice.call(arguments, 1)];
          return originalResolveFilename.apply(this, modifiedArguments);
        }
      }
       /***********************Temporary fix end***************************/
        return originalResolveFilename.apply(this, arguments);
      };
      return () => {
        Module._resolveFilename = originalResolveFilename;
      };
    }

mandarzope avatar Jun 12 '24 14:06 mandarzope

+1. Also failed with "Node16".

shrinktofit avatar Jul 13 '24 16:07 shrinktofit

still getting this...

RogutKuba avatar Jul 24 '24 17:07 RogutKuba

This is a patch that works with Svelte 5, note that package managers such as bun and pnpm offer patching generally through the bun patch command:

diff --git a/bin.cjs b/bin.cjs
index 96235f52f7d0fcaed6abb588107a9fed948bd808..95d4eabc57367e599ceef4185c5e2fa7cedb4e0c 100755
--- a/bin.cjs
+++ b/bin.cjs
@@ -17138,6 +17138,16 @@ var require_node2 = __commonJS({
             return originalResolveFilename.apply(this, modifiedArguments);
           }
         }
+       /***********************Temporary fix start***************************/
+
+      if (request.endsWith('.js')) {
+        const found = require('path').resolve(_parent.path, request.replace('.js', '.ts'))
+        if (require('fs').existsSync(found)) {
+          const modifiedArguments = [found, ...[].slice.call(arguments, 1)];
+          return originalResolveFilename.apply(this, modifiedArguments);
+        }
+      }
+       /***********************Temporary fix end***************************/
         return originalResolveFilename.apply(this, arguments);
       };
       return () => {

This is not an ideal solution but "it just works"

Shyrogan avatar Aug 13 '24 17:08 Shyrogan

Appreciate the report @mandarzope. Same issue here.

drizzle-kit: 0.24.2.

The above fix didn't work for me (and I'd like to avoid patching libraries that I'll only break again when upgrading / re-installing).

Instead I pointed drizzle.config.ts to the compiled schema.js file instead of the uncompiled schema.ts file. In my case:

// tsconfig.json
"compilerOptions": {
  "rootDir": "src",
  "outDir": "dist",
  ...
}

and then

// OLD drizzle.config.ts
export default defineConfig({
  ...
  schema: "./src/db/schema.ts",
  ...
})
// NEW, FIXED drizzle.config.ts
export default defineConfig({
  ...
  schema: "./dist/db/schema.js",
  ...
})

Not a great fix! Surely someone will come up with something smarter. But you can make it a little nicer by tweaking package.json to run tsc first (see npm docs on pre/post scripts):

// package.json
"scripts": {
  ...
  "pregenerate-sql": "tsc",
  "generate-sql": "drizzle-kit generate",
  "premigrate-sql": "tsc",
  "migrate-sql": "drizzle-kit migrate"
  ...
}

FWIW: drizzle-kit generate/migrate were working exactly as expected until now. Unclear which commit broke it -- the only recent change to my tsconfig.json was adding skipLibCheck: true (and alas, that flag was only turned on because of the ts compile errors thrown by drizzle-orm).

But take that with a grain of salt: doesn't make sense that skipLibCheck would break this, and reverting it to false didn't fix either. But it's the only hint I could glean from our commit history.

misium avatar Sep 06 '24 17:09 misium

Also the same issue here with "skipLibCheck": true, to disable Drizzle ORM warnings, and "moduleResolution": "NodeNext",

We're using @misium 's suggested workaround with the following package.json

"scripts": {
  "clean": "rm -rf node_modules build dist .turbo",
  "build": "tsc -p tsconfig.json",
  "start": "node dist/index.js",
  "test": "tsx --test $(find . -name \"*.test.ts\")",
  "dev": "tsx --no-warnings --env-file=.env --watch ./index.ts | pino-pretty --colorize",
  "drizzle:generate": "tsc -p tsconfig.json && drizzle-kit generate --config=drizzle.config.ts",
  "drizzle:migrate": "drizzle-kit migrate --config=drizzle.config.ts",
  "drizzle:seed": "tsx --no-warnings --env-file=.env ./database/drizzle/seeds/index.ts"
},

And our drizzle.config.ts as....

import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  dialect: 'postgresql',
  schema: './dist/database/drizzle/schema/index.js',
  out: './database/drizzle/migrations',
  dbCredentials: {
    url: process.env.POSTGRES_CONNECTION_STRING as string,
  },
  verbose: true,
  strict: true,
})

For completeness, here's our tsconfig.json file....

{
  "compilerOptions": {
    "lib": ["es2020"],
    "module": "NodeNext",
    "target": "es2020",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "incremental": true,
    "noImplicitAny": false,
    "moduleResolution": "NodeNext",
    "sourceMap": true,
    "outDir": "./dist",
    "skipLibCheck": true,
    "baseUrl": "./",
    "paths": {
      "config/*": ["config/*"],
      "@/*": ["./src/*"]
    },
    "types": ["node"]
  },
  "include": ["index.ts", "fastify.d.ts", "config", "src", "test"],
  "exclude": ["node_modules", "dist", "coverage"]
}

58bits avatar Sep 10 '24 19:09 58bits

I have the same issue (I have "module": "node16", "moduleResolution": "node16", and "skipLibCheck": true). I tried using the compiled file as per @misium's comment above, and it solved the problem for me.

mosheduminer avatar Nov 18 '24 21:11 mosheduminer

This appears to be related to https://github.com/drizzle-team/drizzle-orm/issues/1228 which was recently marked as resolved by @AndriiSherman without explanation.

joshuamcginnis avatar Dec 26 '24 17:12 joshuamcginnis

To get around this, you can run the script with something like tsx:

pnpm dlx tsx node_modules/drizzle-kit/bin.cjs generate

sarahrobichaud avatar Jan 26 '25 21:01 sarahrobichaud

The issue is still there.

At the expense of being unpopular: I got around this by switching back to module resolution: bundler and module to esnext. I build with tsc and ts-alias and add the file extensions during build with ts-alias. I found this to be the best approach for me (but maybe not right for folks deeply invested in the native node resolution). In theory, having native node resolution leads to faster start times but in practice it's just annoying.

Steps:

  1. Change all file paths in one go:

Search regex: from "([^"]+?)(/index)?.js" Replace: from "$1"

  1. Change tsconfig.json
{
  "compilerOptions": {
    [...],
    "module": "esnext",
    "moduleResolution": "bundler"
  },
  "tsc-alias": {
    "resolveFullPaths": true
  }
}
  1. Add build script to package.json tsc && tsc-alias

florianmartens avatar Apr 25 '25 11:04 florianmartens

Hey everyone!

I've created this message to send in a batch to all opened issues we have, just because there are a lot of them and I want to update all of you with our current work, why issues are not responded to, and the amount of work that has been done by our team over ~8 months.

I saw a lot of issues with suggestions on how to fix something while we were not responding – so thanks everyone. Also, thanks to everyone patiently waiting for a response from us and continuing to use Drizzle!

We currently have 4 major branches with a lot of work done. Each branch was handled by different devs and teams to make sure we could make all the changes in parallel.


First branch is drizzle-kit rewrite

All of the work can be found on the alternation-engine branch. Here is a PR with the work done: https://github.com/drizzle-team/drizzle-orm/pull/4439

As you can see, it has 167k added lines of code and 67k removed, which means we've completely rewritten the drizzle-kit alternation engine, the way we handle diffs for each dialect, together with expanding our test suite from 600 tests to ~9k test units for all different types of actions you can do with kit. More importantly, we changed the migration folder structure and made commutative migrations, so you won't face complex conflicts on migrations when working in a team.

What's left here:

  • We are finishing handling defaults for Postgres, the last being geometry (yes, we fixed the srid issue here as well).
  • We are finishing commutative migrations for all dialects.
  • We are finishing up the command, so the migration flow will be as simple as drizzle-kit up for you.

Where it brings us:

  • We are getting drizzle-kit into a new good shape where we can call it [email protected]!

Timeline:

  • We need ~2 weeks to finish all of the above and send this branch to beta for testing.

Second big branch is a complex one with several HUGE updates

  • Bringing Relational Queries v2 finally live. We've done a lot of work here to actually make it faster than RQBv1 and much better from a DX point of view. But in implementing it, we had to make another big rewrite, so we completely rewrote the drizzle-orm type system, which made it much simpler and improved type performance by ~21.4x:
(types instantiations for 3300 lines production drizzle schema + 990 lines relations)

TS v5.8.3: 728.8k -> 34.1k
TS v5.9.2: 553.7k -> 25.4k

You can read more about it here.

What's left here:

Where it brings us:

  • We are getting drizzle-orm into a new good shape where we can call it [email protected]!

Breaking changes:

  • We will have them, but we will have open channels for everyone building on top of drizzle types, so we can guide you through all the changes.

Third branch is adding support for CockroachDB and MSSQL dialects

Support for them is already in the alternation-engine branch and will be available together with the drizzle-kit rewrite.

Summary

All of the work we are doing is crucial and should be done sooner rather than later. We've received a lot of feedback and worked really hard to find the best strategies and decisions for API, DX, architecture, etc., so we can confidently mark it as v1 and be sure we can improve it and remain flexible for all the features you are asking for, while becoming even better for everyone building on top of the drizzle API as well.

We didn't want to stay with some legacy decisions and solutions we had, and instead wanted to shape Drizzle in a way that will be best looking ahead to 2025–2026 trends (v1 will get proper effect support, etc.).

We believe that all of the effort we've put in will boost Drizzle and benefit everyone using it.

Thanks everyone, as we said, we are here to stay for a long time to build a great tool together!

Timelines

We are hoping to get v1 for drizzle in beta this fall and same timeline for latest. Right after that we can go through all of the issues and PRs and resond everyone. v1 for drizzle should close ~70% of all the bug tickets we have, so on beta release we will start marking them as closed!

AndriiSherman avatar Aug 30 '25 18:08 AndriiSherman