In Node.js if you are using ES modules (by either having an .mjs
/ .mts
file, or adding "type": "module"
in the package.json
), importing the various drizzle-orm
modules has to be done differently than when using commonjs, and from how it is shown in the docs.
As an example, in order to get the imports to work if using node-postgres
, the imports need to look like this when using esm:
import { pgTable } from 'drizzle-orm/pg-core/index.js';
import { drizzle } from 'drizzle-orm/node-postgres/index.js';
import { eq } from 'drizzle-orm/expressions.js';
import { migrate } from 'drizzle-orm/node-postgres/migrator/index.js';
import pg from 'pg';
const { Pool } = pg;
A way to resolve this would be to declare all the package entry points in the package.js using the exports
field. To test this solution out I modified the package.json to add an exports
field with the entry points for the imports I showed above. This is what I added:
"exports": {
".": "./index.js",
"./expressions": "./expressions.js",
"./pg-core": "./pg-core/index.js",
"./node-postgres": "./node-postgres/index.js",
"./node-postgres/migrator": "./node-postgres/migrator/index.js"
}
These changes allowed me to change the imports to how they are documented in the docs (besides for pg
), like this:
import { pgTable } from 'drizzle-orm/pg-core';
import { drizzle } from 'drizzle-orm/node-postgres';
import { eq } from 'drizzle-orm/expressions';
import { migrate } from 'drizzle-orm/node-postgres/migrator';
import pg from 'pg';
const { Pool } = pg;
I may be able to help with a PR for this if this is something you would want to have done. I am not sure what all the entry points are that you want to have exposed though. If you want to be less strict though, you can set the exports
with wildcards as seen in that Node.js docs link I posted above.
If this does get fixed, you may want to also consider updating your docs to show how to make importing Pool
from pg
work when using esm, e.g.
import pg from 'pg';
const { Pool } = pg;
I was using drizzle-orm v0.17.6 and Node.js v18.13.0.
Thanks for all you guys do! This project is amazing, and all your hard work is very appreciated!