kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.44k stars 266 forks source link

ERR_MODULE_NOT_FOUND error while running migration in nextjs #436

Closed ma13gagan closed 1 year ago

ma13gagan commented 1 year ago

Hey

While I am running the migration in a nextjs ts project, I am getting the below error

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'src' imported from /<path-to-project>/node_modules/kysely/dist/esm/migration/file-migration-provider.js
    at new NodeError (node:internal/errors:372:5)
    at packageResolve (node:internal/modules/esm/resolve:954:9)
    at moduleResolve (node:internal/modules/esm/resolve:1003:20)
    at defaultResolve (node:internal/modules/esm/resolve:1218:11)
    at ESMLoader.resolve (node:internal/modules/esm/loader:580:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:294:18)
    at ESMLoader.import (node:internal/modules/esm/loader:380:22)
    at importModuleDynamically (node:internal/modules/esm/translators:106:35)
    at importModuleDynamicallyCallback (node:internal/process/esm_loader:35:14)
    at FileMigrationProvider.getMigrations (file:///<path-to-project>/node_modules/kysely/dist/esm/migration/file-migration-provider.js:33:60) {
  code: 'ERR_MODULE_NOT_FOUND'
}

I am using the PlanetScale Serverless Driver for the prject. But for the migrations I am using the inbuilt MysqlDialect. Below is the migration script.

import * as path from "path"
import { promises as fs } from "fs"
import { Kysely, Migrator, FileMigrationProvider, MysqlDialect } from "kysely"
import { createPool } from "mysql2"
import { config } from "dotenv"

config()

async function migrateToLatest() {
    const db = new Kysely<any>({
        dialect: new MysqlDialect({
            pool: createPool({
                host: process.env.DATABASE_HOST,
                user: process.env.DATABASE_USERNAME,
                password: process.env.DATABASE_PASSWORD,
                database: process.env.DATABASE,
                ssl: {},
            }),
        }),
    })

    const migrator = new Migrator({
        db,
        provider: new FileMigrationProvider({
            fs,
            path,
            migrationFolder: "./src/server/migrations",
        }),
    })

    const { error, results } = await migrator.migrateToLatest()

    results?.forEach((it) => {
        if (it.status === "Success") {
            console.log(`migration "${it.migrationName}" was executed successfully`)
        } else if (it.status === "Error") {
            console.error(`failed to execute migration "${it.migrationName}"`)
        }
    })

    if (error) {
        console.error("failed to migrate")
        console.error(error)
        process.exit(1)
    }

    await db.destroy()
}

migrateToLatest()

I build this file with tsc -t ES2015 --moduleResolution node scripts/migrate.ts

igalklebanov commented 1 year ago

Hey 👋

Can you provide a repository that can reproduce this?

ma13gagan commented 1 year ago

Hi @igalklebanov,

Here is the repo link nextjs-kysely-migration

koskimas commented 1 year ago

You need to use an absolute path to the built javascript migrations. You are using a relative path to the typescript migrations. Node can't run typescript.