serverless-nextjs / serverless-next.js

⚡ Deploy your Next.js apps on AWS Lambda@Edge via Serverless Components
MIT License
4.47k stars 457 forks source link

Custom lambda handler can't be found - cannot resolve files if next.config is in a different folder to serverless #84

Open barrysteyn opened 5 years ago

barrysteyn commented 5 years ago

Hi

Using node10.x. Here is my serverless.yml

service: project

plugins:
  - serverless-nextjs-plugin
  - serverless-domain-manager
  - serverless-offline

custom:
  # Our stage is based on what is passed in when running serverless
  # commands. Or fallsback to what we have set in the provider section.
  stage: ${self:provider.stage}
  prefixes:
    RESOURCES_PREFIX: ${self:service.name}-${self:provider.stage}
  serverless-nextjs:
    nextConfigDir: ./client/
    staticDir: ./client/static
    customHandler: ./lambda-handler.js
    pageConfig:
      categories:
        events:
          - http:
              path: categories/
              method: GET
          - http:
              path: categories/{slug}
              method: GET
      episode:
        events:
          - http:
              path: episode/
              method: GET
          - http:
              path: episode/{slug}
              method: GET
  customDomain:
    domainName: project-name
    basePath: ''
    stage: ${self:custom.stage}
    createRoute53Record: true

provider:
  name: aws
  runtime: nodejs10.x
  memorySize: 512
  timeout: 10
  stage: ${env:STAGE, 'staging'}
  tags:
    SERVICE: ${self:custom.prefixes.RESOURCES_PREFIX}
  region: ${opt:region, 'us-east-1'}

package:
  exclude:
    - ./**/*

However, I get an error on AWS:

Endpoint response body before transformations: {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module '../lambda-handler.js'","trace":["Runtime.ImportModuleError: Error: Cannot find module '../lambda-handler.js'","    at Object.module.exports.load (/var/runtime/UserFunction.js:86:13)","    at Object.<anonymous> (/var/runtime/index.js:23:30)","    at Module._compile (internal/modules/cjs/loader.js:701:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)","    at Module.load (internal/modules/cjs/loader.js:600:32)","    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)","    at Function.Module._load (internal/modules/cjs/loader.js:531:3)","    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)","    at startup (internal/bootstrap/node.js:283:19)","    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)"]}
Mon May 27 21:13:18 UTC 2019 : Lambda execution failed with status 200 due to customer function error: Error: Cannot find module '../lambda-handler.js'. Lambda request id: 66fc2ce2-e479-4ea7-b12f-4c22fb94260c
Mon May 27 21:13:18 UTC 2019 : Method completed with status: 502

The crazy thing is that this works perfectly when I deploy it locally using serverless-offline. It also builds perfectly. I have been forced to comment out the custom lambda.

Any ideas?

danielcondemarin commented 5 years ago

@barrysteyn Can you try rewriting your custom routes with the new routes api (https://github.com/danielcondemarin/serverless-nextjs-plugin#custom-page-routing):

Instead of pageConfig use routes. Something like:

routes:
  - src: categories
    path: categories
  - src: categories
    path: categories/{slug}
    request:
          parameters:
            paths:
              slug: true
   - src: episode
     ...  

Might not fix the issue but is worth a try.

barrysteyn commented 5 years ago

Unfortunately, using routes still elicits the same problem. To give you some insight, the root folder (where package.json is found and the serverless.yml) has a sub-folder called client where the next config is placed. That is where my custom lambda is found. So like this:

package.json
serverless.yml
client/
    next.config.js
    custom-lambda-handler.js

@danielcondemarin perhaps I could share my repo with you if this would help.

danielcondemarin commented 5 years ago

Ah I bet this is because your serverless.yml isn’t in the same dir as next.config.js. There aren’t many tests to cover this. The temporary workaround would be to put serverless.yml in the same dir as next config. I’ll have a look into getting this fixed.

barrysteyn commented 5 years ago

I will try test later on today. But it used to work just fine in the past.

barrysteyn commented 5 years ago

Moving serverless.yml to the same directory as next.config.js fixes the problem. This is just to confirm this. I will probably wait for the fix (thanks so much for doing this).

barrysteyn commented 5 years ago

One last comment: It works fine with serverless-offline without me moving serverless.yml to same folder as next.config.js

That's kinda strange? @danielcondemarin the reason why this is happening kept me thinking last night, and then I realized I could just ask you :)

barrysteyn commented 5 years ago

@danielcondemarin Another problem we have is trying to route for static assets...

Our setup is like this:

  serverless-nextjs:
    nextConfigDir: ./client/
    staticDir: ./client/static
    customHandler: ./lambda-handler.js
    routes:
      - src: ./client/static/sw.js
        path: sw.js
      - src: category
        path: category/{slug}

Both the customHandler and sw.js cannot be resolved.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.