Does Drizzle ORM support CommonJS (CJS)?
I'm trying to use Drizzle ORM for the first time in a brand new project and I am getting this error:
36 Replies
I know that in the TypeScript ecosystem, it is common for libraries to support both CJS and ESM.
Is this the case with Drizzle ORM?
Can't you just use
.mts
CJS files can't import .mts files, right?
I am stuck on CJS because I have dependencies that are on CJS.
which 💀
Konva to start with, I'm sure plenty of others
rip
isnt cjs with
require
not import thoughNo
CJS is if you have "commonjs" in package.json
type
fieldno you dont need it its default
so you need to be doing require? no?
No... not in TypeScript. Anyways, this isn't a thread about discussing the differences between CJS and ESM, let's stick to the issue at hand please.
const { pgTable, serial, text, varchar } = await import("drizzle-orm/pg-core")
Might work?CJS can't use await at the top level.
what if u get rid of the await then lol
Then the promise doesn't resolve. That won't work.
oh well im out of ideas now
you sol
what about
drizzle-kit@cjs
npm i drizzle-kit@cjs
I'm not using drizzle kit. This question is about drizzle ORM.
And
drizzle-orm@cjs
doesn't exist.ah my bad
You can always fallback to
.then
in commonjs
and stick to callbacks
Or, create an async function where you do all your async stuff and call it afterwardsyes, Drizzle supports both CJS and ESM.
Do you know why I'm getting this error then? Should I try again from a fresh TypeScript CJS project?
Checke if you have "type": "module" in your package.json
See this; https://discordapp.com/channels/1043890932593987624/1139574925452132512/1139599752552513698
I don't - I'm using commonJS. I don't understand what you are asking.
I can't change to module because then I wouldn't be using CJS, obviously.
I haven't tested this but I think you can do
or
You can take a look at the library source code and you'll find that they have an
index.cjs
, index.mjs
and an index.d.ts
for every submodule they have.
Now that I think about it, you should be able to do:
With no issuesThat "works" but not really, because the types are any.
Using
require
is deprecated, we should be using import
in all brand new TypeScript code now.
Is there a way to make it work properly with TypeScript and not have the types be any
?Can you share your package.json and your tsconfig? This should work
Well, it's a monorepo, and in your packages/server folder there is no package.json
I'll try in a brand new TypeScript project.
I just did, and it works properly
Your file is .ts, so it will be transpiled. You should be able to use import without problems
I honestly don
don't know if it'll work without a package.json
There is a package.json, I just linked it to you.
It's extremely common in monorepos to not have package.json files per package and only have one at the root of the repository.
Don't you need a package.json for every package in your monorepo?
This is what e.g. Nx recommends, which is the biggest monorepo tool in the world.
I don't have a lot of experience in monorepo setups
But I just setup a new commonjs project, installed typescript, setup a tsconfig and imported drizzle the ESM way and I had types and all
That leads me to believe it's your setup
Hrm, you are right, it does seem to work fine in a brand new TypeScript project.
I was able to narrow down the problem. If you add the recommended tsconfig for node to a brand new TypeScript project, then you get the CommonJS error from the OP.
More specifically, all you have to do is to put
"module": "Node16",
into your TSConfig, and drizzle fails to import.
@bloberenober @angelelz Does Drizzle not support modern node projects that use CJS?
Node16
is a recommended setting for all brand new Node projects.
As documented here: https://www.typescriptlang.org/tsconfig#module
and here: https://github.com/tsconfig/bases/blob/main/bases/node-lts.json
Simply by specifying this setting causes Drizzle to fail to import.Honestly, I'm not sure what exactly needs to be changed to fix that. If you have any ideas, please share, it'll greatly improve the chances of the fix being implemented sooner rather than later.
I've never saw people have issues with CJS projects so far, only ESM.