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;
}
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.
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
}
}
{
"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
}
}
4 Replies
ben
benā€¢2y ago
Reviving this thread because I'm also experiencing this issue and haven't found a way to fix it Any luck?
Zee_008
Zee_008OPā€¢2y ago
No, I stopped working on this! It was a hustle
shaw93
shaw93ā€¢2y ago
Dave Gray
YouTube
Build a Random Quote Machine with Nextjs, PlanetScale MySQL, & Driz...
Web Dev Roadmap for Beginners (Free!): https://bit.ly/DaveGrayWebDevRoadmap Build a random quote machine with Nextjs, PlanetScale MySQL, & Drizzle ORM. In this tutorial, we'll set up a MySQL database on PlanetScale, connect to and query the database with Drizzle ORM, and build a Next.js frontend and API routes. šŸš© Subscribe āžœ https://bit.ly/3n...
iukea
iukeaā€¢2y ago
Also it is asking for a dynamic important (kinda strange but also looks like you are windows) - recommend learning WSL on windows
Want results from more Discord servers?
Add your server