Skip to content

The "express" dependency was not found. Did you install "express" it in your source folder via npm. #14

@benalexb

Description

@benalexb

Hello dear friends,

I was hoping that maybe you could give me a clue as to what I am doing wrong. I deviated from using the template app as I wanted to try this solution on a more realistic use case: deploying an existing express app to AWS lambda. From the original REST API I created just to test this out, the only changes I really had to make in order to have things similar to the template app were

  • Decouple HTTP server and express app (such that there is an app.js that exports the express app)
  • Added webpack to bundle the app and make it smaller

The bundle goes to ./build containing two files (1) app.js and (2) server.js, the latter being irrelevant for what we are trying to accomplish.

As the title shows, when I hit the endpoint after deploying it, I get the following response The "express" dependency was not found. Did you install "express" it in your source folder via npm.

On the AWS Lambda editor, inspecting the code that was deployed, I can see where this response comes from

Screenshot 2020-06-02 at 13 18 30

App.js

import 'regenerator-runtime/runtime'
import bodyParser from 'body-parser'
import cookieParser from 'cookie-parser'
import cors from 'cors'
import express from 'express'
import morgan from 'morgan'
import router from './router'

const app = express()
const isProduction = process.env.NODE_ENV === 'production'

// App setup
app.use(morgan(isProduction ? 'combined' : 'dev'))
app.use(cors())
app.use(cookieParser())
app.use(bodyParser.json({ type: '*/*' }))
router(app)

export default app

serverless.yml

component: express
name: express-api
stage: dev

inputs:
  description: fine-sifter-server-REST
  region: eu-central-1
  env:
    DEBUG: 'express:*'
    DB_CONN: (...)
    CLIENT_SECRET: (...)
  src:
    src: ./
    hook: npm run build:local
    dist: ./build

webpack.config.js

const path = require('path')
const nodeExternals = require('webpack-node-externals')

module.exports = {
  entry: {
    app: './src/app.js',
    server: './src/server.js'
  },
  mode: process.env.NODE_ENV,
  target: 'node',
  externals: [nodeExternals()],
  output: {
    libraryTarget: 'commonjs',
    path: path.resolve(__dirname, 'build'),
    filename: '[name].js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: [['env', { targets: { node: '12.17' } }]]
            }
          }
        ]
      }
    ]
  }
}

package.json

{
  "name": "fine-sifter-server-rest",
  "version": "0.0.1",
  "description": "",
  "author": "Benjamin Alexandre de Macedo Barreto",
  "license": "ISC",
  "main": "src/index.js",
  "engines": {
    "node": ">=12.17.0",
    "npm": ">=6.14.5"
  },
  "scripts": {
    "build": "NODE_ENV=production webpack",
    "build:local": "env-cmd -f env-prod.env webpack",
    "start": "node build/server.js",
    "dev": "env-cmd -f env-dev.env nodemon --exec babel-node src/server.js",
    "dev:docker": "env-cmd -f env-docker-dev.env nodemon --exec babel-node src/server.js",
    "docker:dev": "docker-compose -f docker-compose.yml -f docker-compose.dev.yml up --build",
    "docker:prod": "",
    "db:up": "docker-compose up --build -d",
    "db:down": "docker-compose down",
    "db:seed": "babel-node src/db-tools/seed.js"
  },
  "devDependencies": {
    "@babel/cli": "^7.10.1",
    "@babel/core": "^7.10.2",
    "@babel/node": "^7.10.1",
    "@babel/preset-env": "^7.10.2",
    "babel-loader": "^8.1.0",
    "babel-preset-env": "^1.7.0",
    "env-cmd": "^10.1.0",
    "nodemon": "^2.0.4",
    "regenerator-runtime": "^0.13.5",
    "serverless": "^1.71.3",
    "standard": "^14.3.4",
    "webpack": "^4.43.0",
    "webpack-cli": "^3.3.11",
    "webpack-node-externals": "^1.7.2"
  },
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.19.0",
    "cookie-parser": "^1.4.5",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "faker": "^4.1.0",
    "jwt-simple": "^0.5.6",
    "lodash": "^4.17.15",
    "mongoose": "^5.9.16",
    "morgan": "^1.10.0",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0"
  },
  "nodemonConfig": {
    "ignore": [
      "dist",
      "node_modules"
    ]
  },
  "standard": {
    "parser": "babel-eslint",
    "ignore": [
      ".dist",
      "node_modules"
    ]
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions