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?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
bensiu
  • 24,660
  • 56
  • 77
  • 117
  • This may help you, put `module` as shown here: https://stackoverflow.com/q/61401475/8816585 – kissu Sep 27 '22 at 05:36

1 Answers1

11

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/

Istvan
  • 7,500
  • 9
  • 59
  • 109
Francisco Ramos
  • 196
  • 2
  • 7
  • My man, you saved me from hell. Took me hours and hours!! – Markus Nov 25 '22 at 01:15
  • 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` – Sudarshan Anbazhagan May 23 '23 at 12:09