DT
Drizzle Team•16mo ago
Zamiel

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:
No description
36 Replies
Zamiel
ZamielOP•16mo ago
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?
barry
barry•16mo ago
Can't you just use .mts
Zamiel
ZamielOP•16mo ago
CJS files can't import .mts files, right? I am stuck on CJS because I have dependencies that are on CJS.
barry
barry•16mo ago
which 💀
Zamiel
ZamielOP•16mo ago
Konva to start with, I'm sure plenty of others
barry
barry•16mo ago
rip isnt cjs with require not import though
Zamiel
ZamielOP•16mo ago
No CJS is if you have "commonjs" in package.json type field
barry
barry•16mo ago
no you dont need it its default
barry
barry•16mo ago
No description
barry
barry•16mo ago
so you need to be doing require? no?
Zamiel
ZamielOP•16mo ago
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.
barry
barry•16mo ago
const { pgTable, serial, text, varchar } = await import("drizzle-orm/pg-core") Might work?
Zamiel
ZamielOP•16mo ago
CJS can't use await at the top level.
barry
barry•16mo ago
what if u get rid of the await then lol
Zamiel
ZamielOP•16mo ago
Then the promise doesn't resolve. That won't work.
barry
barry•16mo ago
oh well im out of ideas now you sol what about drizzle-kit@cjs npm i drizzle-kit@cjs
Zamiel
ZamielOP•16mo ago
I'm not using drizzle kit. This question is about drizzle ORM. And drizzle-orm@cjs doesn't exist.
barry
barry•16mo ago
ah my bad
Angelelz
Angelelz•16mo ago
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 afterwards
Dan
Dan•16mo ago
yes, Drizzle supports both CJS and ESM.
Zamiel
ZamielOP•16mo ago
Do you know why I'm getting this error then? Should I try again from a fresh TypeScript CJS project?
Angelelz
Angelelz•16mo ago
Checke if you have "type": "module" in your package.json See this; https://discordapp.com/channels/1043890932593987624/1139574925452132512/1139599752552513698
Zamiel
ZamielOP•16mo ago
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.
Angelelz
Angelelz•16mo ago
I haven't tested this but I think you can do
const { pgTable, serial, text, varchar } = require("drizzle-orm/pg-core/index")
const { pgTable, serial, text, varchar } = require("drizzle-orm/pg-core/index")
or
const { pgTable, serial, text, varchar } = require("drizzle-orm/pg-core/index.cjs")
const { pgTable, serial, text, varchar } = require("drizzle-orm/pg-core/index.cjs")
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:
const { pgTable, serial, text, varchar } = require("drizzle-orm/pg-core")
const { pgTable, serial, text, varchar } = require("drizzle-orm/pg-core")
With no issues
Zamiel
ZamielOP•16mo ago
That "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?
Angelelz
Angelelz•16mo ago
Can you share your package.json and your tsconfig? This should work
Angelelz
Angelelz•16mo ago
Well, it's a monorepo, and in your packages/server folder there is no package.json
Zamiel
ZamielOP•16mo ago
I'll try in a brand new TypeScript project.
Angelelz
Angelelz•16mo ago
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
Zamiel
ZamielOP•16mo ago
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.
Angelelz
Angelelz•16mo ago
Don't you need a package.json for every package in your monorepo?
Zamiel
ZamielOP•16mo ago
This is what e.g. Nx recommends, which is the biggest monorepo tool in the world.
Angelelz
Angelelz•16mo ago
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
Zamiel
ZamielOP•16mo ago
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.
Dan
Dan•16mo ago
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.
Want results from more Discord servers?
Add your server