Serverless framework deploy does not install node dependancies

I have a very simple node app which I got from the serverless examples: examples/aws-node-express-api at master · serverless/examples · GitHub. I deployed with:

serverless deploy

serverless.yml is:

service: aws-node-express-api

frameworkVersion: '2'


provider:
  name: aws
  runtime: nodejs12.x
  lambdaHashingVersion: '20201221'

functions:
  api:
    handler: handler.handler
    events:
      - http:
          path: /
          method: ANY
      - http:
          path: /{proxy+}
          method: ANY

handler.js is:

const serverless = require("serverless-http");
const express = require("express");
const app = express();

app.get("/", (req, res, next) => {
  return res.status(200).json({
    message: "Hello from root!",
  });
});

app.get("/hello", (req, res, next) => {
  return res.status(200).json({
    message: "Hello from path!",
  });
});

app.use((req, res, next) => {
  return res.status(404).json({
    error: "Not Found",
  });
});

module.exports.handler = serverless(app);

package.json is:

{
  "name": "aws-node-express-api",
  "version": "1.0.0",
  "description": "",
  "dependencies": {
    "express": "^4.17.1",
    "serverless-http": "^2.7.0"
  }
}

When I run a CURL, I get:

{"message": "Internal server error"}

When I check the logs I see:

{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module 'serverless-http'\nRequire stack:\n- /var/task/handler.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module 'serverless-http'",
        "Require stack:",
        "- /var/task/handler.js",
        "- /var/runtime/UserFunction.js",
        "- /var/runtime/index.js",
        "    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:43:30)",
        "    at Module._compile (internal/modules/cjs/loader.js:999:30)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)",
        "    at Module.load (internal/modules/cjs/loader.js:863:32)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:708:14)",
        "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)",
        "    at internal/main/run_main_module.js:17:47"
    ]
}

I went to the Lambda function on AWS and I see:

enter image description here

So you can see there is no node_modules folder. I dont want to push up the node_modules folder, I expect the dependancies to get installed automtically (Im pretty sure this happened before when I last create a serverless node function). But it’s not getting installed. How can I ensure dependancies get installed?

Before running serverless deploy, did you run
npm i

Is there a node_modules folder in your project before deploying it?

serverless deploy is just going to package up what’s in your project folder.

thanks for the information.