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
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 studiocommand.So far I’ve thought of two different workarounds:
In your drizzle.config.ts file, set the
schemapath to start from theoutDirfrom your tsconfig.This should work since the outDir will only contain
.jsfiles 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.tsshould 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.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.tsmodule, which CANNOT have any.jsimports 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.
You’re a lifesaver! That’s so simple!
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
distfolder for compiled schemas.distfolder from tsconfig.drizzle-cli.jsonExplanation Every time you run, for example,
push:dev, firstly, your schema files are compiled to thedistfolder (which is, in my case, located in the same schema folder). Then drizzle-kit will not use your source schema files with.tsextension but compiled.jsfiles. I hope this helps someone.Another workaround is to use different module settings in your tsconfig file.
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
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.