3

I try to deploy simple Nuxt 3 application to AWS Lambda for SSR. So far I have:

in nitro.config.ts

import { defineNitroConfig } from 'nitropack';

export default defineNitroConfig({
  preset: 'aws-lambda',
  serveStatic: true
});

in Lambda handler

import { handler } from './output/server/index.mjs';

export const runner = (event, context) => {
  const { statusCode, headers, body } = handler({ rawPath: '/' });

  return {
    statusCode,
    body,
    headers
  }
}

in serverless.yaml

functions:
  ssr:
    handler: handler.runner
    timeout: 15
    package:
      individually: true
      include:
        - output/**
    events:
      - httpApi:
          path: '*'
          method: '*'

I run yarn build, change .output folder name to output to be able to include it with package, but I still have errors like "errorMessage": "SyntaxError: Cannot use import statement outside a module".

Is someone has idea how it could be done?

1

1 Answer 1

12

Its easier than that bro, nuxt already exports the handler nothing for us to do:

serverles.yml:

service: nuxt-app-lamba

provider:
  name: aws
  runtime: nodejs14.x
  stage: dev
  region: us-east-1

functions:
  nuxt:
    handler: .output/server/index.handler
    events:
      - httpApi: '*'

nuxt.config.ts:

// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
    nitro: {
        preset: 'aws-lambda',
        serveStatic: true
      }
})

package.json:

{
  "private": true,
  "scripts": {
    "build": "nuxt build",
    "build:lamba": "NITRO_PRESET=aws-lambda nuxt build",
    "dev": "nuxt dev",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare",
    "deploy": "sls deploy"
  },
  "devDependencies": {
    "nuxt": "3.0.0-rc.11"
  }
}

This example is not working anymore (2022.12.09):

Working example: https://8cyysz2g5f.execute-api.us-east-1.amazonaws.com/

Sign up to request clarification or add additional context in comments.

2 Comments

My man, you saved me from hell. Took me hours and hours!!
Greetings with the latest version of nuxt js I get the output generated as .output/server/index.mjs. hence I get the error like this Cannot find module './.output/server/index.js' Require stack: - /var/task/s_nuxt.js - /var/runtime/index.mjs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.