Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Cannot deploy my next.js application to AWS Beanstalk

박성민 June 20, 2023

Hi folks, I'm struggling with deploying my app to AWS Beanstalk.

 

To begin, I'd like show some of my current source codes.

 

*bitbucket pipeline


image: node:16

pipelines:
default:
- parallel:
- step:
name: Build and Test
caches:
- node
script:
- npm install
- npm test
- step:
name: Code linting
script:
- npm install eslint
- npx eslint .
caches:
- node
branches:
main:
- step:
name: Build v1
caches:
- node
script:
- rm -rf node_modules
- rm -f package-lock.json
- apt-get update
- apt-get install zip
- npm install
- npm run build
- zip -r application.zip . -x node_modules/\* src/\* .git/\*
- ls -LR
artifacts:
- "application.zip"
- step:
name: Deploy to Production
trigger: manual
deployment: Production
script:
- pipe: atlassian/aws-elasticbeanstalk-deploy:1.1.0
variables:
AWS_ACCESS_KEY_ID: $BITBUCKET_PIPELINE_ACCESS_KEY
AWS_SECRET_ACCESS_KEY: $BITBUCKET_PIPELINE_SECRET_KEY
AWS_DEFAULT_REGION: 'ap-northeast-2'
APPLICATION_NAME: 'care-mono-app'
ENVIRONMENT_NAME: 'care-admin-client-master'
S3_BUCKET: $S3_BUCKET_DEPLOY
ZIP_FILE: 'application.zip'

*package.json

{
"name": "unicare-admin",
"version": "0.1.0",
"private": true,
"engines": { "node" : "v16.20.0" },
"scripts": {
"dev": "next dev",
"build": "next build",
"export": "next export",
"start": "next start -p 8081",
"lint": "next lint"
},
"dependencies": {
"@next/font": "13.4.6",
"@next/swc-linux-x64-gnu": "^13.3.0",
"@nextui-org/react": "^1.0.0-beta.12",
"@types/next-auth": "^3.15.0",
"@types/node": "18.11.19",
"@types/react": "^18.0.27",
"@types/react-dom": "18.0.10",
"eslint": "8.33.0",
"eslint-config-next": "13.1.6",
"next": "13.1.6",
"next-auth": "^4.21.1",
"react-dom": "18.2.0",
"typescript": "4.9.5"
},
"devDependencies": {
"@babel/core": "^7.21.4",
"@babel/preset-env": "^7.21.4",
"@babel/preset-typescript": "^7.21.4",
"@reduxjs/toolkit": "^1.9.5",
"@types/react-redux": "^7.1.25",
"flag-icons": "^6.7.0",
"next-google-fonts": "^2.2.0",
"next-themes": "^0.2.1",
"node-sass": "^7.0.3",
"react-display-name": "^0.2.5",
"react-redux": "^8.0.5",
"sass": "^1.61.0",
"sass-loader": "^13.2.2"
}
}
As far as I understood, I need to make a ZIP file to deploy to AWS Beanstalk using bitbucket pipeline. 
After finishing the pipeline works, the deployment itself is finished successfully, but I cannot run my app properly.
I can find this kind of error messages from logs.

Jun 21 01:58:17 ip-172-31-14-187 web: > unicare-admin@0.1.0 start

Jun 21 01:58:17 ip-172-31-14-187 web: > next start

Jun 21 01:58:17 ip-172-31-14-187 web: node:internal/modules/cjs/loader:1029

Jun 21 01:58:17 ip-172-31-14-187 web: throw err;

Jun 21 01:58:17 ip-172-31-14-187 web: ^

Jun 21 01:58:17 ip-172-31-14-187 web: Error: Cannot find module '../build/output/log'

Jun 21 01:58:17 ip-172-31-14-187 web: Require stack:

Jun 21 01:58:17 ip-172-31-14-187 web: - /var/app/current/node_modules/.bin/next

Jun 21 01:58:17 ip-172-31-14-187 web: at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1026:15)

Jun 21 01:58:17 ip-172-31-14-187 web: at Function.Module._load (node:internal/modules/cjs/loader:871:27)

Jun 21 01:58:17 ip-172-31-14-187 web: at Module.require (node:internal/modules/cjs/loader:1098:19)

Jun 21 01:58:17 ip-172-31-14-187 web: at require (node:internal/modules/cjs/helpers:108:18)

Jun 21 01:58:17 ip-172-31-14-187 web: at Object.<anonymous> (/var/app/current/node_modules/.bin/next:3:35)

Jun 21 01:58:17 ip-172-31-14-187 web: at Module._compile (node:internal/modules/cjs/loader:1196:14)

Jun 21 01:58:17 ip-172-31-14-187 web: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1250:10)

Jun 21 01:58:17 ip-172-31-14-187 web: at Module.load (node:internal/modules/cjs/loader:1074:32)

Jun 21 01:58:17 ip-172-31-14-187 web: at Function.Module._load (node:internal/modules/cjs/loader:909:12)

Jun 21 01:58:17 ip-172-31-14-187 web: at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {

Jun 21 01:58:17 ip-172-31-14-187 web: code: 'MODULE_NOT_FOUND',

Jun 21 01:58:17 ip-172-31-14-187 web: requireStack: [ '/var/app/current/node_modules/.bin/next' ]

Jun 21 01:58:17 ip-172-31-14-187 web: }

Jun 21 03:42:18 ip-172-31-14-187 web: > unicare-admin@0.1.0 start

Jun 21 03:42:18 ip-172-31-14-187 web: > next start -p 8081

Jun 21 03:42:18 ip-172-31-14-187 web: ready - started server on 0.0.0.0:8081, url: http://localhost:8081

Jun 21 03:42:18 ip-172-31-14-187 web: info  - Loaded env from /var/app/current/.env

 

Would you give me a hint to solve this problem?

Is it even possible to deploy to AWS Beanstalk using bitbucket pipeline with NextJS?

 

Regards,

Chris

 

 

 

 

* update #1

I figured that Bitbucket don't make ".next" folder when I run the script "npm run build".

I think ".next" directory is for the build output which is necessary. How can I make the folder with script?

1 answer

1 accepted

0 votes
Answer accepted
박성민 June 21, 2023

I figured it out to rewrite my build script like below

 

script:
- rm -rf node_modules
- rm -rf package-lock.json
- apt-get update
- apt-get install zip
- npm cache clean --force
- npm install
- npm run build
- zip -r application.zip . #-x node_modules/\* src/\* .git/\*
- ls -al

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events