Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'getCompilationErrors' of undefined

I'm having issues setting up an express server instance on serverless with nextJs. I keep getting a Cannot read property 'getCompilationErrors' of undefined when running the server function. It seems to be an issue with app.render.

  • When running debug it seems to be coming from within nextJs

Server.js

const express = require('express');
const path = require('path');
const dev = process.env.NODE_ENV !== 'production';
const next = require('next');
const pathMatch = require('path-match');

const app = next({ dev });
const handle = app.getRequestHandler();
const { parse } = require('url');

const server = express();
const route = pathMatch();

server.use('/_next', express.static(path.join(__dirname, '.next')));

server.get('/', (req, res) => app.render(req, res, '/'));

server.get('*', (req, res) => handle(req, res));


module.exports = server; 

index.js


const sls = require('serverless-http')
const binaryMimeTypes = require('./binaryMimeTypes')

const server = require('./server')

module.exports.server = sls(server, {
    binary: binaryMimeTypes
});

Serverless.yml


service: ssr-react-next

provider:
  name: aws
  runtime: nodejs8.10
  stage: ${self:custom.secrets.NODE_ENV}
  region: us-east-1
  environment: 
    NODE_ENV: ${self:custom.secrets.NODE_ENV}

functions:
  server:
    handler: index.server
    events:
      - http: ANY /
      - http: ANY /{proxy+}

plugins:
  - serverless-apigw-binary
  - serverless-domain-manager

custom:
  secrets: ${file(secrets.json)}
  apigwBinary:
    types:
      - '*/*'
  customDomain:
    domainName: ${self:custom.secrets.DOMAIN}
    basePath: ''
    stage: ${self:custom.secrets.NODE_ENV}
    createRoute53Record: true
    # endpointType: 'regional'
    # if the ACM certificate is created in a region except for `'us-east-1'` you need `endpointType: 'regional'`
like image 203
Thomas Davis Avatar asked Dec 15 '25 14:12

Thomas Davis


1 Answers

Figured a way around this, just needed to prepare the app with async

   server.use(async(req, res, next) => {
       await app.prepare();
       next();
   })
like image 95
Thomas Davis Avatar answered Dec 19 '25 05:12

Thomas Davis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!