yarbsemaj / sveltekit-adapter-lambda

An adapter to build a SvelteKit app into a lambda ready for deployment with lambda proxy via the Serverless Framework or CDK.
https://www.npmjs.com/package/@yarbsemaj/adapter-lambda
MIT License
77 stars 16 forks source link

Build errors #2

Closed eric-naguras closed 2 years ago

eric-naguras commented 2 years ago

Hi, Thanks for making this adapter, I'd love to use it.

I have a very simple Sveltekit app that only has endpoints, no assets or anything static. When I try to build it with your adapter I get the following build error: `

Using adapter-serverless Cannot read properties of undefined (reading 'log') at adapt (/workspace/crm-backend-sveltekit-aws-lambda/node_modules/@yarbsemaj/adapter-lambda/index.js:27:21) at adapt (file:///workspace/crm-backend-sveltekit-aws-lambda/node_modules/@sveltejs/kit/dist/chunks/index5.js:740:8) at file:///workspace/crm-backend-sveltekit-aws-lambda/node_modules/@sveltejs/kit/dist/cli.js:994:11`

The app works without problems with 'svelte-kit dev" and using the Cloudflare adapter it also runs without issues on Cloudflare Workers.

You can find the repo here : https://github.com/NagurasDemoApp/crm-backend-sveltekit-aws-lambda

If you open the repo with this link https://gitpod.io/#https://github.com/NagurasDemoApp/crm-backend-sveltekit-aws-lambda you wil get a VSCode instance in the browser and can look at the error by doing a build in the integrated terminal from VSCode.

If you run the app in dev mode you can test if it's working by opening the the file login.http in the folder http-tests. On the top line you should see "Send Request". By clicking on that line, it will send a request to the app and you will see a return. Works a bit like Postman by using this extension (which will be available already if you open the repo via Gitpod.

Thanks.

yarbsemaj commented 2 years ago

Hi @eric-naguras ! Thank You for your interest in my project and bringing this issue to my attention. The issue you are experiencing look to be due to a change in the svelte kit adapter API between when I created this adapter, and the present day. They mention the possibility of this on there website. I will issue an updated version of the adapter shortly to resolve these issues

yarbsemaj commented 2 years ago

@eric-naguras I have updated the adapter for the latest svelte kit build, it should now work for your project

eric-naguras commented 2 years ago

Thank you James for updating the package. It works like a charm now.

To deploy I used the AWS CDK. I'm including my CDK stack file below. You might want to add this to your README.md for people who don't know the Serverless framework or who don't like it. AWS CDK is free and easy to use.

  1. Create a CDK project (see https://docs.aws.amazon.com/cdk/v2/guide/hello_world.html)
  2. Create a directory "resources" inside the CDK project
  3. Copy the generated files from /build/server into resources
  4. Copy the code below in the ...stack.js file in the project's lib folder
  5. You will need to change some of the class and variable names
  // This is Version 2 of the CDK
  const { Stack } = require('aws-cdk-lib');
  const { Construct } = require("constructs");
  const apigateway = require("aws-cdk-lib/aws-apigateway");
  const lambda = require("aws-cdk-lib/aws-lambda");

class CrmBackendServiceStack extends Stack {
  /**
   *
   * @param {Construct} scope
   * @param {string} id
   * @param {StackProps=} props
   */
  constructor(scope, id, props) {
    super(scope, id, props);

    // The code that defines your stack goes here
    new CRMBackendService(this, 'CRMBackend')

  }
}

class CRMBackendService extends Construct {
  constructor(scope, id) {
    super(scope, id);

    const handler = new lambda.Function(this, "CRMBackendHandler", {
      runtime: lambda.Runtime.NODEJS_14_X, // So we can use async in widget.js
      code: lambda.Code.fromAsset("resources"), // Copy files from /build/server into resources
      handler: "serverless.handler",
      environment: {}
    });

    new apigateway.LambdaRestApi(this, 'crmbackend-api', {
      handler: handler,
      description: "This service serves crmbackend.",
      restApiName: "CRMBackend Service",
      proxy: true
    });

  }
}

module.exports = { CrmBackendServiceStack }
yarbsemaj commented 2 years ago

@eric-naguras thanks for the update, I will take your consideration on board re documentation, Iv also updated the package again to support routing via a lambda@edge function and included the s3 upload as part of the serverless config. As such is now possible to deploy your code with one command.