4
votes

I had a simple Serverless website running in AWS lambda using node serverless deploy, I then added more stuff to the site and updated to the latest version of serverless, now when I deploy the site and visit the URL I get:

{"message": "Internal server error"}

When I visit the lambda console I get:

{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module './dist/server'",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module './dist/server'",
    "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:45:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:778:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)",
    "    at Module.load (internal/modules/cjs/loader.js:653:32)",
    "    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:585:3)",
    "    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)",
    "    at startup (internal/bootstrap/node.js:283:19)"
  ]
}

Any idea how to fix this?

More Info

Based on @Amit Baranes comment below which references this SO post here is a Angular site. I don't quite understand what I need to change the handler name to to match my site structure. I am not bundling the files into a .zip myself, but am running the serverless deploy command through node:

Here is the website serverless.yml

# generated by @ng-toolkit/serverless
service: serverless-site

plugins:
  - serverless-apigw-binary

provider:
  name: aws
  runtime: nodejs10.x
  memorySize: 192
  timeout: 10
  stage: production
  region: us-east-1

package:
  exclude:
   - src/**
   - node_modules/**
   - firebug-lite/**
   - e2e/**
   - coverage/**
   - '!node_modules/aws-serverless-express/**'
   - '!node_modules/binary-case/**'
   - '!node_modules/type-is/**'
   - '!node_modules/media-typer/**'
   - '!node_modules/mime-types/**'
   - '!node_modules/mime-db/**'

custom:
  apigwBinary:
    types:
      - '*/*'

functions:
  api:
    handler: lambda.universal
    events:
      - http: ANY {proxy+}
      - http: ANY /

Here is my project file structure (starting inside the src directory) for the website.

enter image description here enter image description here

Here is the website lambda function when it is deployed:

enter image description here

Since the function didn't have a server.js. I attempted to create is by modifying server.ts and putting it in the folder with no luck:

// generated by Paul
//import 'zone.js/dist/zone-node';
//import 'reflect-metadata';

var express =require('express');
var cors= require('cors');
var compression =require('compression');

var join=require('path').join;

export const app = express();

app.use(compression());
app.use(cors());

const DIST_FOLDER = join(process.cwd(), 'dist/serverless-site');

app.get('*.*', express.static(join(DIST_FOLDER), {
    maxAge: '1y'
}));

app.get('/*', (req, res) => {
    res.sendFile(join(DIST_FOLDER + '/index.html'));
});
2
Check that file name and handler name are same stackoverflow.com/questions/41750026/…Amit Baranes
@AmitBaranes Any idea what the handler name for an Angular site would be?Rilcon42
Try lambda.js...Amit Baranes
Tried that, but got the same error unfortunatelyRilcon42
Sorry for the late response, any updaet?Amit Baranes

2 Answers

0
votes

So it seems that server.js is not created in the dist folder.

I got it working by running npm run build:server:prod before serverless deploy

The relevant line in my package.json looks as follows:

"build:server:serverless": "webpack --config webpack.server.config.js"

This is probably not the right way to do it, but it solved the issue of not finding ./dist/server and got me to the next error:

{
    "errorType": "TypeError",
    "errorMessage": "express is not a function",
    "stack": [
        "TypeError: express is not a function",
        "    at Object.<anonymous> (/var/task/dist/server.js:9231:15)",
        "    at __webpack_require__ (/var/task/dist/server.js:21:30)",
        "    at /var/task/dist/server.js:85:18",
        "    at Object.<anonymous> (/var/task/dist/server.js:88:10)",
        "    at Module._compile (internal/modules/cjs/loader.js:778:30)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)",
        "    at Module.load (internal/modules/cjs/loader.js:653:32)",
        "    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:585:3)",
        "    at Module.require (internal/modules/cjs/loader.js:692:17)"
    ]
}

I got the express error fixed by setting esModuleInterop = false in the tsconfig file, thanks to AWS Serverless Lambda + Angular - TypeError: express is not a function

-1
votes

I do see you are using the ng-toolkit package in order to provide universal + serverless for your Angular App.

Based on that, I do think you are receiving such error due to either bad config on latest version of the package, which includes some bugs on the server file along with mismatch with the Angular/Universal package.

I do recommend checking out my sample repo in the meantime Example Angular ng-toolkit, which already fixes that.

Just to point out: this is caused by webpack.config option externals which was introduced on latest express-engine schematic of the Angular Universal packages. Commenting that line, will fix your issue.

The reason is still unknown for me at the moment but hopefully I will bring my findings on this topic.

Cheers!