floydspace / serverless-esbuild

💨 A Serverless framework plugin to bundle JavaScript and TypeScript with extremely fast esbuild
MIT License
445 stars 137 forks source link

How to integrate prisma with serverless framework aws-nodejs-typescript template that uses serverless-esbuild #332

Closed kaykhan closed 2 years ago

kaykhan commented 2 years ago

I am building a serverless function using the serverless framework. However im having an issue with running it locally

Error: ENOENT: no such file or directory, open ''/.esbuild/.build/node_modules/.prisma/client/schema.prisma'

prisma/schema.prisma

generator client {
    provider      = "prisma-client-js"
    binaryTargets = ["native", "rhel-openssl-1.0.x"]
}

serverless.ts

package: {
    individually: true,
    patterns: [
        "!node_modules/.prisma/client/libquery_engine-*",
        "node_modules/.prisma/client/libquery_engine-rhel-*",
        "!node_modules/prisma/libquery_engine-*",
        "!node_modules/@prisma/engines/**",
    ],
},

steps:

npx prisma generate && npm install 

sls invoke local -f main

What am i doing wrong here?

note:

floydspace commented 2 years ago

Hi @kaykhan. if you're using default serverless-esbuild config, it will bundle everything for you in one file, but to correctly resolve .prisma schema you'll need to let esbuild know hot to treat the schema file, so could you try to define:

  esbuild:
    loader:
      .prisma: text

and let us know if it helps

kaykhan commented 2 years ago

@floydspace

Not sure what the difference is between what your suggesting and below.

I added the schema.prisma in the following and it works.

    package: {
        individually: true,
        patterns: [
            "!node_modules/.prisma/client/libquery_engine-*",
            "node_modules/.prisma/client/schema.prisma",
            "node_modules/.prisma/client/libquery_engine-debian-*",
            "!node_modules/prisma/libquery_engine-*",
            "!node_modules/@prisma/engines/**",
        ],
    },
floydspace commented 2 years ago

the difference would be that in my example it would put your schema in the bundle during the build step and would not demand the file in runtime.

your solution also works because you copied the schema file in the place where it's expected in runtime and resolves correctly.