diff --git a/bin/node-pg-migrate.ts b/bin/node-pg-migrate.ts index 49f920a8..7290956f 100755 --- a/bin/node-pg-migrate.ts +++ b/bin/node-pg-migrate.ts @@ -21,10 +21,7 @@ process.on('uncaughtException', (err) => { process.exit(1); }); -const crossRequire = createRequire( - // @ts-expect-error: ignore until esm only - import.meta.url || __dirname -); +const crossRequire = createRequire(resolve('_')); function tryRequire(moduleName: string): TModule | null { try { diff --git a/src/migration.ts b/src/migration.ts index f6a2f982..36266a46 100644 --- a/src/migration.ts +++ b/src/migration.ts @@ -8,8 +8,7 @@ import { createReadStream, createWriteStream } from 'node:fs'; import { mkdir, readdir } from 'node:fs/promises'; -import { createRequire } from 'node:module'; -import { basename, dirname, extname, join, resolve } from 'node:path'; +import { basename, extname, resolve } from 'node:path'; import type { QueryResult } from 'pg'; import type { DBConnection } from './db'; import MigrationBuilder from './migrationBuilder'; @@ -135,23 +134,12 @@ export class Migration implements RunMigration { ? now.toISOString().replace(/\D/g, '') : now.valueOf(); - const crossRequire = createRequire( - // @ts-expect-error: ignore until esm only - import.meta.url || __dirname - ); - const moduleDir = dirname( - crossRequire.resolve('node-pg-migrate/package.json') - ); - const templateFileName = 'templateFileName' in options ? resolve(process.cwd(), options.templateFileName) : resolve( - moduleDir, - join( - 'templates', - `migration-template.${await resolveSuffix(directory, options)}` - ) + 'node_modules/node-pg-migrate/templates', + `migration-template.${await resolveSuffix(directory, options)}` ); const suffix = getSuffixFromFileName(templateFileName); diff --git a/src/runner.ts b/src/runner.ts index b0c0da49..729da38d 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -1,4 +1,5 @@ -import { extname, relative } from 'node:path'; +import { createRequire } from 'node:module'; +import { extname, resolve } from 'node:path'; import type { DBConnection } from './db'; import Db from './db'; import type { RunMigration } from './migration'; @@ -35,11 +36,11 @@ async function loadMigrations( const migrations = await Promise.all( files.map(async (file) => { - const filePath = `${options.dir}/${file}`; + const filePath = resolve(options.dir, file); const actions: MigrationBuilderActions = extname(filePath) === '.sql' ? await migrateSqlFile(filePath) - : require(relative(__dirname, filePath)); + : createRequire(resolve('_'))(filePath); shorthands = { ...shorthands, ...actions.shorthands }; return new Migration(