serverless-bundle icon indicating copy to clipboard operation
serverless-bundle copied to clipboard

Bundling prisma2 with the plugin

Open ADrejta opened this issue 5 years ago • 7 comments

Hi,

I'm having trouble initializing prisma when using the serverless-bundle plugin

Prisma needs to run a script "prisma generate" and uses a schema.prisma file (that i need to copy into the webpack bundle process) in order to make the client to access the database. I have added this part in the custom options of the plugin like so:

custom:
  bundle:
    copyFiles:
      - from: "prisma/schema.prisma"
        to: "./"
    packagerOptions:
      scripts:
        - prisma generate

But then when I run serverless offline and i execute an api i get this error: Failed to execute scheduled function: [hello] Error: Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again

It looks as though the script is not being run. Is there a way to see logs and see what is happening during the bundling process? Maybe the script is failing and it would be easier if i could see the output of the script.

Node Version : 14.0.0 NPM Version: 6.9.0 Serverless Bundle version: 3.2.1

ADrejta avatar Dec 07 '20 11:12 ADrejta

I think the script is not running.

I tried switching it to this:

custom:
  bundle:
    tsConfig: "tsconfig.json"
    copyFiles:
      - from: "./prisma/schema.prisma"
        to: "./"
    packagerOptions:
      scripts:
        - "cat > sample.txt"

Then when i look at the .webpack folder there is no file named sample.txt. P.S While the script is not being run, the copyFiles part is working since i can see the schema.prisma file inside the .webpack folder P.S.S Could it also be that the script might start before the copying of the file?

ADrejta avatar Dec 07 '20 13:12 ADrejta

Did you managed to get this working?

davidboom95 avatar Dec 24 '20 03:12 davidboom95

Did you managed to get this working?

Sadly I didn't. I had to switch to serverless-webpack plugin to get this working

ADrejta avatar Dec 24 '20 08:12 ADrejta

And you replaced serverless-bundle or you are using both of them?

davidboom95 avatar Dec 24 '20 13:12 davidboom95

No i replaced it with the serverless-webpack and am not using the serverless-bundle plugin at the moment. Sorry for not being clear about that.

I do plan on revisiting this issue since maintaining webpack.config files can be a hassle at times. But since I couldn't figure it out for my use case at the moment I had to switch to serverless-webpack.

ADrejta avatar Dec 24 '20 15:12 ADrejta

We are currently relying on serverless-webpack for the custom scripts: https://github.com/serverless-heaven/serverless-webpack#custom-scripts

Does anybody want to take a look why it's not being invoked?

jayair avatar Jan 03 '21 02:01 jayair

I came back to this and tried this setup with these configs: Node version : 14.0.0 serverless: 2.33.1 serverless-bundle: 3.0.0

Looks like prisma has to be added in to the externals sections in order to work properly

custom:
   bundle:
      externals:
         - "@prisma/client"
      copyFiles:
         - from: "prisma/schema.prisma"
           to: "./"
      packagerOptions:
         scripts:
            - prisma generate

I think adding prisma to the default list of externals would be a good idea in this case. This should probably be closed now since adding prisma to the externals options does do the trick, but i will leave it open in case it is used as a reference to add prisma to the default externals list or something.

ADrejta avatar Apr 05 '21 09:04 ADrejta