Home > front end >  Cannot find any nodejs modules AWS Elastic Beanstalk
Cannot find any nodejs modules AWS Elastic Beanstalk

Time:09-13

I'm trying to deploy my express server to AWS Elastic Beanstalk but keep running into the problem of modules not being found. I am using CodeBuild and a buildspec.yml to build and convert my code to an artifact.

EB Deployment Log:

----------------------------------------
/var/log/web.stdout.log
----------------------------------------
Sep 12 22:54:02 ip-172-31-15-80 web: at Function.Module._load (node:internal/modules/cjs/loader:839:12)
Sep 12 22:54:02 ip-172-31-15-80 web: at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
Sep 12 22:54:02 ip-172-31-15-80 web: code: 'MODULE_NOT_FOUND',
Sep 12 22:54:02 ip-172-31-15-80 web: requireStack: [ '/var/app/current/app.js' ]
Sep 12 22:54:02 ip-172-31-15-80 web: }
Sep 12 22:54:02 ip-172-31-15-80 web: node:internal/modules/cjs/loader:959
Sep 12 22:54:02 ip-172-31-15-80 web: throw err;
Sep 12 22:54:02 ip-172-31-15-80 web: ^
Sep 12 22:54:02 ip-172-31-15-80 web: Error: Cannot find module 'express'
Sep 12 22:54:02 ip-172-31-15-80 web: Require stack:
Sep 12 22:54:02 ip-172-31-15-80 web: - /var/app/current/app.js
Sep 12 22:54:02 ip-172-31-15-80 web: at Function.Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
Sep 12 22:54:02 ip-172-31-15-80 web: at Function.Module._load (node:internal/modules/cjs/loader:804:27)
Sep 12 22:54:02 ip-172-31-15-80 web: at Module.require (node:internal/modules/cjs/loader:1028:19)
Sep 12 22:54:02 ip-172-31-15-80 web: at require (node:internal/modules/cjs/helpers:102:18)
Sep 12 22:54:02 ip-172-31-15-80 web: at Object.<anonymous> (/var/app/current/app.js:6:35)
Sep 12 22:54:02 ip-172-31-15-80 web: at Module._compile (node:internal/modules/cjs/loader:1126:14)
Sep 12 22:54:02 ip-172-31-15-80 web: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
Sep 12 22:54:02 ip-172-31-15-80 web: at Module.load (node:internal/modules/cjs/loader:1004:32)
Sep 12 22:54:02 ip-172-31-15-80 web: at Function.Module._load (node:internal/modules/cjs/loader:839:12)
Sep 12 22:54:02 ip-172-31-15-80 web: at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
...

Buildspec.yml

version: 0.2

batch:
  fast-fail: true
phases:
  install:
    runtime-versions:
      nodejs: 16
  build:
    commands:
      - npm run build
artifacts:
  type: zip
  files:
    - "**/*"
  base-directory: build

Any help would be greatly appreciated.

CodePudding user response:

You need to copy the node_modules in the build step. The way I do it is adding a post_build pahase.

post_build:
    commands:
        - cp -R node_modules/ dist/node_modules
  • Related