Error importing @planetscale/database

I am trying to setup my drizzle orm, but I am having an issue with the "@planetscale/database" module. Heres the error message:
Error: require() of ES Module
C:\<PATH>\node_modules\@planetscale\database\dist\index.js from
C:\<PATH>\db\connect.ts not supported.
Instead change the require of index.js in
C:\<PATH>\db\connect.ts to a dynamic import() which is available in all CommonJS modules.
Error: require() of ES Module
C:\<PATH>\node_modules\@planetscale\database\dist\index.js from
C:\<PATH>\db\connect.ts not supported.
Instead change the require of index.js in
C:\<PATH>\db\connect.ts to a dynamic import() which is available in all CommonJS modules.
This happens when I run the following in my vercel serverless function in both production and dev mode:
import 'dotenv/config'
import { connect } from "@planetscale/database";
import { drizzle } from "drizzle-orm/planetscale-serverless";

const conn = connect({
host: process.env.DB_HOST!,
username: process.env.DB_USERNAME!,
password: process.env.DB_PASSWORD!,
});
export const db = drizzle(conn);
import 'dotenv/config'
import { connect } from "@planetscale/database";
import { drizzle } from "drizzle-orm/planetscale-serverless";

const conn = connect({
host: process.env.DB_HOST!,
username: process.env.DB_USERNAME!,
password: process.env.DB_PASSWORD!,
});
export const db = drizzle(conn);
Following the setups outlined here: https://orm.drizzle.team/docs/installation-and-db-connection/mysql/planetscale I tried to switch the import to a dynamic one but had issues related to importing in other modules as I had to then change my tsconfig settings. Here is a stack overflow post by another person describing the same issue in nearly the same way: https://stackoverflow.com/questions/76424056/planetscale-database-produces-an-error-with-drizzle-in-hapi-with-ts-node-in Please help I have spent a few hours looking for solutions and have no idea how to fix it. Also if anyone has any reading recommendations on the intricacies on nodes module system please let me know!
Stack Overflow
@planetscale/database produces an error with drizzle (in Hapi, with...
I am using Drizzle ORM and Planetscale. Connecting to Planetscale I get an error: Error [ERR_REQUIRE_ESM]: require() of ES Module /node_modules/@planetscale/database/dist/index.js from src/database/
1 Reply
mootshrooms
mootshrooms17mo ago
I did more testing without drizzle in the same project directory with the following code:
import "dotenv/config"
import { connect } from '@planetscale/database'

const conn = connect({
host: process.env.DB_HOST,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
});

(async () => {
const results = conn.execute('select 1 from dual where 1=?', [1]);
console.log(await results)
})();
import "dotenv/config"
import { connect } from '@planetscale/database'

const conn = connect({
host: process.env.DB_HOST,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
});

(async () => {
const results = conn.execute('select 1 from dual where 1=?', [1]);
console.log(await results)
})();
Based on code from: https://github.com/planetscale/database-js But got the same error. Therefore it seems like it's purely an issue with the way I am using the @planetscale/database module. I think it might be an issue with my tsconfig target. Does this seem like a likely culprit? Any recommendations on fixes or where I may be able to find fixes?
GitHub
GitHub - planetscale/database-js: A Fetch API-compatible PlanetScal...
A Fetch API-compatible PlanetScale database driver - GitHub - planetscale/database-js: A Fetch API-compatible PlanetScale database driver
Want results from more Discord servers?
Add your server