adamjkb / bark

Materialized path extension for Prisma
https://prisma-extension-bark.gitbook.io
MIT License
33 stars 5 forks source link

Error: No "exports" main defined in prisma-extension-bark/package.json #60

Closed donnguyen closed 10 months ago

donnguyen commented 10 months ago

Hi there,

We are looking forward to trying this out but couldn't import it in our NestJS app. Can you please declare main in package.json file and publish a new release?

Error: No "exports" main defined in node_modules/prisma-extension-bark/package.json
    at new NodeError (node:internal/errors:399:5)
    at exportsNotFound (node:internal/modules/esm/resolve:361:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:641:13)
    at resolveExports (node:internal/modules/cjs/loader:565:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:634:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1061:27)
    at Function.Module._load (node:internal/modules/cjs/loader:920:27)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)

Thank you

adamjkb commented 10 months ago

It sounds NestJS is running in commonjs but this extension currently only supports ESM at the moment. I'm not too familiar with how NestJS apps are built but it seems like there is a compilation step anyway so would it be possible to switch to ESM for your application?

I've created a quick demo app in stackblitz with the latest version of NestJS and it seems to be running fine.

donnguyen commented 10 months ago

Thanks, @adamjkb. Can you save me the demo app on stackblitz please?

donnguyen commented 10 months ago

Actually, never mind. It works with "moduleResolution": "node16", added to typescript config. Cheers

donnguyen commented 10 months ago

Just share what we did here in case anyone needs it:

import { PrismaClient } from '@prisma/client';
import { type withBark } from 'prisma-extension-bark';

async function getWithBark(): Promise<typeof withBark> {
  const module = await (eval(`import('prisma-extension-bark')`) as Promise<any>);
  return module.withBark;
}

export const getPrismaClient = async () => {
  const withBark = await getWithBark();

  return new PrismaClient({
    log: ['query', 'info', 'warn'],
  }).$extends(withBark({ modelNames: ['folder'] }));
};

export type ExtendedPrismaClient = Awaited<ReturnType<typeof getPrismaClient>>;