Typescript error when importing the planetscale-drizzle db instance.

my db is hosted on planetscale and im trying to do the setup as explained in the drizzle docs but when I import db instance i get the following error.

My code
import * as schema from './migrations/schema';
import { InsertUser, usersTable } from './schema/user';

const { drizzle } = require('drizzle-orm/planetscale-serverless');
const { connect } = require('@planetscale/database');

const connection = connect({
  host: process.env.DATABASE_HOST,
  username: process.env.DATABASE_USERNAME,
  password: process.env.DATABASE_PASSWORD
});

export const drizzleDb = drizzle(connection, { schema });

export async function createUser(insertUser: InsertUser) {
  const user = await drizzleDb.insert(usersTable).values(insertUser).execute();

  return user;
}

Error
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/mohamedziyad/Desktop/Janit/backend/node_modules/.pnpm/@planetscale+database@1.7.0/node_modules/@planetscale/database/dist/index.js from /Users/mohamedziyad/Desktop/Janit/backend/src/db/index.ts not supported.
Instead change the require of index.js in /Users/mohamedziyad/Desktop/Janit/backend/src/db/index.ts to a dynamic import() which is available in all CommonJS modules.


tsConfig
{
  "compilerOptions": {
    "target": "ES2020",
    "rootDir": "./",
    "baseUrl": "./",
    "module": "commonjs",
    "outDir": "./build",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "moduleResolution": "node",
    "lib": ["es2020"],
    "types": ["node", "dotenv"],
    //
    "noEmit": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "incremental": true
  }
}
Was this page helpful?