blitz-js / legacy-framework

MIT License
3 stars 2 forks source link

Deployment on AWS: TypeError: Cannot read property '$create' of undefined #385

Closed jatidevelopments closed 2 years ago

jatidevelopments commented 2 years ago

What is the problem?

I have successfully deployed BlitzJS to AWS. However, when I try to login or signup an account I get an Error: Blitz.JS Deployment: TypeError: Cannot read property '$create' of undefined

I think this issue is related to blitz-js/legacy-framework#13

I don't wrap the config like in the issue in 2955, so I don't get why it's not working correctly. BlitzJS is supported with this module, right?

Maybe it has something to do with the next-tf package. I found in "terraform-aws-next-js/packages/tf-next/src/utils/index.ts" that its parsing for next.config.js not blitz.config.js. Maybe because of that blitz can not wrap around next?

Has anybody an idea how I could fix it? Would really appreciate if somebody know a solution!

Paste all your error logs here:

Bildschirmfoto 2021-11-17 um 14 46 15

Paste all relevant code snippets here:

PASTE_HERE (leave the ``` marks)

What are detailed steps to reproduce this?

  1. yarn tf-next
  2. terraform apply

Run blitz -v and paste the output here:

Linux 4.14 | linux-x64 | Node: v16.13.0

blitz: 0.42.4 (local)

  Package manager: yarn 
  System:
    OS: Linux 4.14 Amazon Linux 2
    CPU: (1) x64 Intel(R) Xeon(R) CPU E5-2676 v3 @ 2.40GHz
    Memory: 730.95 MB / 1.94 GB
    Shell: 4.2.46 - /bin/bash
  Binaries:
    Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node
    Yarn: 1.22.17 - /usr/bin/yarn
    npm: 8.1.0 - ~/.nvm/versions/node/v16.13.0/bin/npm
    Watchman: Not Found
  npmPackages:
    @prisma/client: 2.x => 2.30.3 
    blitz: 0.42.4 => 0.42.4 
    prisma: 2.x => 2.30.3 
    react: alpha => 18.0.0-alpha-cb11155c8-20211112 
    react-dom: alpha => 18.0.0-alpha-cb11155c8-20211112 
    typescript: 4.4.4 => 4.4.4 

Please include below any other applicable logs and screenshots that show your problem:

jatidevelopments commented 2 years ago

Related to: https://github.com/milliHQ/terraform-aws-next-js/issues/239

flybayer commented 2 years ago

Hey @jatidevelopments, I'm not sure if we've had anyone use this module with Blitz yet.

In order for Blitz auth to work correctly, we need the .blitz.config.compiled.js file to be available at runtime. This is generated in the project root at build time. The reason it's compiled is because we support blitz.config.ts

Probably the terraform module needs updated to read that file if it detects Blitz.

ofhouse commented 2 years ago

Would agree that this is not an issue with Blitz but with the Terraform module. We currently build with the legacy experimental-serverless-trace target and due to that we always create a wrapper next.config.js that then requires the original config when running tf-next build.

We are in the process of removing the wrapper, so this should be resolved soon.

flybayer commented 2 years ago

Ok, then a workaround that probably would work is use patch-package to change next.config.js in the terraform module to .blitz.config.compiled.js.

jatidevelopments commented 2 years ago

Ok, then a workaround that probably would work is use patch-package to change next.config.js in the terraform module to .blitz.config.compiled.js.

Hi @flybayer thanks for the support!

I patched the tf-next package with patch-package to .blitz.config.compiled.js. Didn't helped.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.findEntryPoint = void 0;
const path_1 = require("path");
const fs_1 = require("fs");
// Looks for a `package.json` or `next.config.js` file in cwd and
// returns the result as a relative path to the basePath
function findEntryPoint(cwd) {

    console.log("CHANGED THE PACKAGE")

    for (const entrypoint of ['package.json', '.blitz.config.compiled.js']) {
        if ((0, fs_1.existsSync)((0, path_1.join)(cwd, entrypoint))) {
            return entrypoint;
        }
    }
    throw new Error(`No package.json or next.config.js could be found in ${cwd}`);
}
exports.findEntryPoint = findEntryPoint;

I think I need to change next.config.js in the terraform module. How am I able to do so? patch-packageis only for npm packages not for terrafrom.

jatidevelopments commented 2 years ago

Hi @ofhouse, could you tell me how long it will take to publish the new version? I need to get it work by next week.

ofhouse commented 2 years ago

I think I need to change next.config.js in the terraform module. How am I able to do so? patch-packageis only for npm packages not for terrafrom.

Quick explainer here: The "module" consists of two parts:

  1. The CLI tf-next
  2. The deployable Terraform code

The CLI creates a package from your Blitz / Next.js source that is consumable by AWS Lambda. Terraform then picks up the package and creates AWS resources from it that are then deployed.

So Terraform only deploys static files generated by the CLI, but does not modify or build any of the Blitz or Next source code. So the CLI ist responsible for producing an invalid package for Blitz here.


Not a big fan of giving ETAs on my Open Source projects since my estimates for update plans were mostly mixed up by my (paid) work in the past 😅 I'll try to take a look at it over the weekend.

jatidevelopments commented 2 years ago

I think I need to change next.config.js in the terraform module. How am I able to do so? patch-packageis only for npm packages not for terrafrom.

Quick explainer here: The "module" consists of two parts:

  1. The CLI tf-next
  2. The deployable Terraform code

The CLI creates a package from your Blitz / Next.js source that is consumable by AWS Lambda. Terraform then picks up the package and creates AWS resources from it that are then deployed.

So Terraform only deploys static files generated by the CLI, but does not modify or build any of the Blitz or Next source code. So the CLI ist responsible for producing an invalid package for Blitz here.

Not a big fan of giving ETAs on my Open Source projects since my estimates for update plans were mostly mixed up by my (paid) work in the past 😅 I'll try to take a look at it over the weekend.

Hmm... Yeah I already thought that the CLI is responsible for building the Lambda Functions. That's why I was confused why changing the next.config.js to the builded blitz.js config as @flybayer commented didn't helped.

Would be great if you could fix it over the weekend. I would maybe give a bug bounty reward if this helps for your motivation ;)

Other than that if you know a workaround for short. Then I would appreciate that too!

ofhouse commented 2 years ago

Learned today that Vercel is still forcing experimental-serverless-trace as build target when it detects Blitz. So using the server target as suggested and implemented by https://github.com/milliHQ/terraform-aws-next-js/pull/89 would not help in this case. I would follow Vercel's evaluation that server target and serverless environment is currently not working for Blitz.

Have you tried to deploy it on Vercel if the error is also present there?