'Why can't express router find my build files on AWS Elastic Beanstalk?
I know this seems like a dumb question, but I've deployed this same app to google and heroku successfully previously, but when I try to deploy to elastic beanstalk using code pipeline it doesn't matter where I direct it to the build files, it always replies with {"errno":-2,"code":"ENOENT","syscall":"stat","path":"/var/app/current/build/index.html","expose":false,"statusCode":404,"status":404}
Is there something about AWS Elastic Beanstalk deployment that I don't know about?
Here is the current address for the build, I have tried '/build' and ".." ,"/build" and pretty much every other combination I can think of to direct to the build files... (I have been stuck on this 2 days now...)
app.use(express.static(path.join(__dirname, "build")));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
In the buildspec file I have asked it to move the build folder to the root directory, because code pipeline does not support moving build file artifacts to folders with a forward slash (i.e. it can't move files into client/build which is where I normally leave the build files).
Build file:
version: 0.2
phases:
pre_build:
commands:
- cd client && npm install && cd ..
- cd server && npm install && cd ..
build:
commands:
- cd client && npm run build && cd ..
post_build:
commands:
- mv ./client/build ./build
artifacts:
secondary-artifacts:
client:
files:
- '**/*'
base-directory: build
server:
files:
- '**/*'
base-directory: server
And here is the output from the build:
[Container] 2022/04/12 00:34:07 Phase complete: PRE_BUILD State: SUCCEEDED
[Container] 2022/04/12 00:34:07 Phase context status code: Message:
[Container] 2022/04/12 00:34:07 Entering phase BUILD
[Container] 2022/04/12 00:34:07 Running command cd client && npm run build && cd ..
> [email protected] build /codebuild/output/src398857100/src/client
> react-scripts build
Creating an optimized production build...
File sizes after gzip:
418.64 kB build/static/js/main.a6efd13b.js
1.77 kB build/static/js/787.8dac752f.chunk.js
541 B build/static/css/main.073c9b0a.css
The project was built assuming it is hosted at /.
You can control this with the homepage field in your package.json.
The build folder is ready to be deployed.
You may serve it with a static server:
npm install -g serve
serve -s build
Find out more about deployment here:
https://cra.link/deployment
[Container] 2022/04/12 00:34:59 Phase complete: BUILD State: SUCCEEDED
[Container] 2022/04/12 00:34:59 Phase context status code: Message:
[Container] 2022/04/12 00:34:59 Entering phase POST_BUILD
[Container] 2022/04/12 00:34:59 Running command mv ./client/build ./build
[Container] 2022/04/12 00:34:59 Phase complete: POST_BUILD State: SUCCEEDED
[Container] 2022/04/12 00:34:59 Phase context status code: Message:
[Container] 2022/04/12 00:34:59 Preparing to copy secondary artifacts client
[Container] 2022/04/12 00:34:59 Expanding base directory path: build
[Container] 2022/04/12 00:34:59 Assembling file list
[Container] 2022/04/12 00:34:59 Expanding build
[Container] 2022/04/12 00:34:59 Expanding file paths for base directory build
[Container] 2022/04/12 00:34:59 Assembling file list
[Container] 2022/04/12 00:34:59 Expanding **/*
[Container] 2022/04/12 00:34:59 Found 14 file(s)
[Container] 2022/04/12 00:34:59 Preparing to copy secondary artifacts server
[Container] 2022/04/12 00:34:59 Expanding base directory path: server
[Container] 2022/04/12 00:34:59 Assembling file list
[Container] 2022/04/12 00:34:59 Expanding server
[Container] 2022/04/12 00:34:59 Expanding file paths for base directory server
[Container] 2022/04/12 00:34:59 Assembling file list
[Container] 2022/04/12 00:34:59 Expanding **/*
[Container] 2022/04/12 00:34:59 Found 12680 file(s)
[Container] 2022/04/12 00:35:02 Phase complete: UPLOAD_ARTIFACTS State: SUCCEEDED
[Container] 2022/04/12 00:35:02 Phase context status code: Message:
The server artifacts are migrating correctly because the server sends responses, but it keeps returning a 404 error when I try to find the index.html file
What am I doing wrong here?
Thanks
Solution 1:[1]
For anyone's information in the future, I had the elastic beanstalk deploy stage in the pipeline only importing only the server artifacts, not all the artifacts lumped into a single folder. That's why it wasn't finding any of the build files, they weren't being imported as an artifact.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | John Shanks |