drizzle-orm: [BUG]: Cannot find module error when import includes '.js' file extension in TypeScript project

What version of drizzle-orm are you using?

0.32.1

What version of drizzle-kit are you using?

0.23.0

Describe the Bug

Attempting to generate a migration script fails with ‘cannot find module’ when a module path includes the ‘.js’ file extension in TypeScript project. Similar issue, if not the same, https://github.com/drizzle-team/drizzle-orm/issues/849

Sample project to recreate the issue https://github.com/Skulltulla/drizzle-kit-mirror-fork/tree/issues/drizzle-issues-0000

Expected behavior

Generating a migration completes successfully when the module import path includes a ‘.js’ file extension.

Environment & setup

5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 37
  • Comments: 22

Most upvoted comments

I have the following script in my package.json: "db:generate": "NODE_OPTIONS='--import tsx' drizzle-kit generate"

I also encountered this issue. Any Idea when a potential fix will be shipped?

I’m also experiencing this issue (drizzle-orm 0.33.0; drizzle-kit 0.24.0) - I ran into it while running the drizzle-kit studio command.

So far I’ve thought of two different workarounds:

  1. In your drizzle.config.ts file, set the schema path to start from the outDir from your tsconfig.

    export default defineConfig({ 
      schema: "./dist/src/db/schema/*",  // replace 'dist' with whatever your outDir is called
      ...
    })
    

    This should work since the outDir will only contain .js files instead of .ts (it seems like the issue is that drizzle-kit looks for a file in your source directory that actually ends with .js). However, this will only work if the files in outDir have the same path as in your source directory (e.g., /src/db/schema/user.ts should compile to /dist/src/db/schema/user.js).

    You will have to rebuild your project every time you make a change to any of the schema source [.ts] files to ensure the schemas provided to drizzle-kit are up to date. You’ll also have to rebuild upon freshly cloning your project since you won’t have committed your outDir folder to version control.

  2. Put all code required by drizzle-kit into one file. I had one file per schema, and in some cases had to import one schema file into another. I worked around the issue by moving all schema/table definitions into one file, eliminating the need for module imports (remember to update your drizzle.config.ts file if you’re doing this). In the sample project provided for this issue, this would involve putting all model classes and schemas into one file.

    The drawback of this approach is that you could end up with a pretty large file and potentially a bunch of loosely related code in the same file depending on how many modules you were previously importing.

Personally, I’ve gone with the second workaround for now. Hopefully this helps if you’re experiencing the same issue.

Can confirm this still happens in Drizzle Kit v0.31.0.

This issue is preventing us from splitting our schemas altogether. We’re forced to include EVERYTHING in a single schema.ts module, which CANNOT have any .js imports that actually reference TypeScript files.

This is a major drag for any big project.

This thread may be of use to people: Generating migration commands programmatically

I think Drizzle needs to expose the ‘generate’ function properly as it does for ‘migrate’ and ‘seed’, which can then allow people to utilize ts-node or tsx to do the parsing and resolve the import statements.

I have the following script in my package.json: "db:generate": "NODE_OPTIONS='--import tsx' drizzle-kit generate"

You’re a lifesaver! That’s so simple!

Another workaround is to use different module settings in your tsconfig file.

{
  "compilerOptions": {
    // ...
    "module": "Preserve",
    "moduleResolution": "bundler",
    // ...
  }
}

Then you should be able to take the file extension types off all your imports (or at least the relevant ones in your schema files for drizzle).

I don’t think this has any negative side effects (besides needing to import cjs modules with a different syntax), but I’m no TS wizard. You’ll have to ask Matt Pocock 😄

It works if you need to run only drizzle-kit commands, however, in my case, it breaks when I try to start my NestJs project – I still need to use file extensions in all imports. My workaround is somewhat similar to https://github.com/drizzle-team/drizzle-orm/issues/2705#issuecomment-2302416946 and allows using separate files for schemas

  1. Create separate tsconfig.drizzle-cli.json. This config should only include your schema files and uses a separate dist folder for compiled schemas.
{
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "nodenext",
    "baseUrl": "./",
    "outDir": "./src/db/schemas/dist",
    "declaration": false,
    "skipLibCheck": true
  },
  "include": ["./src/db/schemas/*.ts"]
}
  1. Modify scripts that call drizzle-kit commands in your package.json
"scripts": {
    "push:dev": "tsc --project ./tsconfig.drizzle-cli.json && drizzle-kit push --config=drizzle-dev.config.ts",
    "gen:dev": "tsc --project ./tsconfig.drizzle-cli.json && drizzle-kit generate --config=drizzle-dev.config.ts",
    "migrate:dev": "tsc --project ./tsconfig.drizzle-cli.json && drizzle-kit migrate --config=drizzle-dev.config.ts",
  },
  1. Modify the path to drizzle schema files in drizzle-dev.config.ts to include dist folder from tsconfig.drizzle-cli.json
export default defineConfig({
  schema: './src/db/schemas/dist/*.schema.js'
});

Explanation Every time you run, for example, push:dev, firstly, your schema files are compiled to the dist folder (which is, in my case, located in the same schema folder). Then drizzle-kit will not use your source schema files with .ts extension but compiled .js files. I hope this helps someone.

Another workaround is to use different module settings in your tsconfig file.

{
  "compilerOptions": {
    // ...
    "module": "Preserve",
    "moduleResolution": "bundler",
    // ...
  }
}

Then you should be able to take the file extension types off all your imports (or at least the relevant ones in your schema files for drizzle).

I don’t think this has any negative side effects (besides needing to import cjs modules with a different syntax), but I’m no TS wizard. You’ll have to ask Matt Pocock 😄

I did

        "db:generate": "dk generate",
        "db:push": "dk push",
        "dk": "NODE_OPTIONS='--import tsx' drizzle-kit"

This thread may be of use to people: Generating migration commands programmatically I think Drizzle needs to expose the ‘generate’ function properly as it does for ‘migrate’ and ‘seed’, which can then allow people to utilize ts-node or tsx to do the parsing and resolve the import statements.

How? I don’t immediately see how to make it work when running drizzle-kit generate.

I guess it can work if you only experience a problem with drizzle-kit generate, so you can manually generate migrations. Otherwise, try my solution if you experience this .js-related error with other drizzle-kit commands too. I have already forgotten about that error and can use drizzle-kit without a headache.

Same issue. Very surprised this isn’t fixed yet.