drizzle-team / drizzle-kit-mirror

Docs and issues repository for drizzle-kit
291 stars 17 forks source link

CLI generate migrations fails with ts module resolution "nodenext" #462

Open mandarzope opened 2 months ago

mandarzope commented 2 months ago

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/drizzle-kit@0.22.7/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/drizzle-kit@0.22.7/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/drizzle-kit@0.22.7/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/drizzle-kit@0.22.7/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;
      };
    }
shrinktofit commented 1 month ago

+1. Also failed with "Node16".

RogutKuba commented 1 month ago

still getting this...

Shyrogan commented 3 weeks ago

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"

misium commented 2 days ago

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.