Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Cannot find module error when import includes '.js' file extension in TypeScript project #2705

Open
Skulltulla opened this issue Jul 29, 2024 · 6 comments
Labels
bug Something isn't working drizzle/kit priority Will be worked on next

Comments

@Skulltulla
Copy link

Skulltulla commented Jul 29, 2024

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, #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
@Skulltulla Skulltulla added the bug Something isn't working label Jul 29, 2024
@thefrana
Copy link

thefrana commented Jul 29, 2024

I also encountered this issue. Since it is recommended to use .js extensions in TypeScript imports, I believe this should be resolved.

Is there any workaround?

@suvanl
Copy link

suvanl commented Aug 21, 2024

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.

@SaadFarooq-Dev
Copy link

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

@L-Mario564 L-Mario564 added drizzle/kit priority Will be worked on next labels Oct 23, 2024
@greatsolace
Copy link

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

@abarreraaponte
Copy link

Same here

@lrsterrett
Copy link

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 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working drizzle/kit priority Will be worked on next
Projects
None yet
Development

No branches or pull requests

8 participants