ERROR with sharp dependency when deploying nodejs app to AWS elastic beanstalk - node.js

i am a beginner in AWS, and I encountered this problem very early on.
I created an EB environment and a code-pipeline in AWS. So whenever I push something to the repository, the app gets deployed. So for now I just have a "Hello world" node.js app, but I want to install the sharp npm dependency for later on. When I put the dependency in the package.json file and push it to the repo, a get the following error:
error on deployment.
I have done a lot of googling, and I think it has something to do with setting permissions to install the sharp dependency. However, none of the solutions I found have worked so far.
If anything is unclear, I apologize and let me know :).

Please reference my "workaround" solution provided in the following GitHub Issue (Fails to install on AWS ElasticBeanstalk with node16 #3221) for a full explanation.
Solution:
Create the following Platform Hooks paths in the root directory of your application bundle.
.platform/hooks/prebuild
.platform/confighooks/prebuild
Create the following bash script (00_npm_install.sh) with execute permissions (chmod +x).
#!/bin/bash
cd /var/app/staging
sudo -u webapp npm install sharp
Validate the Application Bundle Structure.
Ex. Sample project structure:
~/my-app/
├── app.js
├── index.html
├── .npmrc_bkp
├── package.json
├── package-lock.json
├── .platform
│ ├── confighooks
│ │ └── prebuild
│ │ └── 00_npm_install.sh
│ └── hooks
│ └── prebuild
│ └── 00_npm_install.sh
└── Procfile
Deploy the Application!
Hope it helps!

Related

How to add front end to already deployed backend project (same repo)

I currently have a Node.js project with this file structure:
strava-descriptions/
├── .DS_Store
├── .env
├── .eslintrc.js
├── .git
├── .gitignore
├── authorizeStrava.js
├── buildDescription.js
├── config.js
├── db.js
├── getActivity.js
├── getSongsPlayedDuringActivity.js
├── getSpotifyAccessToken.js
├── getSpotifyRecentlyPlayed.js
├── getStravaAccessToken.js
├── getWeatherConditions.js
├── handleWebhookEvent.js
├── node_modules
├── package-lock.json
├── package.json
├── server.js
└── updateDescription.js
The parent folder strava-descriptions is its own GitHub repository and is also deployed to Heroku.
What I want to have happen is to put all of these files into a sub-directory called backend, and create a new frontend folder in strava-descriptions to house a React front end. However, I know that using create-react-app will create its own git repo inside the frontend folder.
My question is, how do I accomplish this without totally messing up my Heroku deployment and current GitHub repo? Ideally I would like to have one complete project on GitHub (frontend and backend together), but have them deployed and functioning as two separate apps.
One approach is to use create-react-app to initialize you project and then just paste the files on your old repository, reorganize the files and then you can manage you builds for react with npm

Setting up docker nodejs application with local npm dependencies

We want to start containerizing our applications, but we have stumbled upon some issues with local dependencies.
We have a single git repository, in which we have numerous node packages, under "shared" folder, and applications that require these packages.
So let's say our folder structure is as follows:
src/
├── apps
│   └── my_app
└── shared
└── shared_module
in my_app package.json we have the following dependency:
{
"dependencies": {
"shared-module": "file:../../shared/shared_module"
}
}
The issue here is that because we want to move "my_app" to run in a container, we need to npm install our local dependency.
Can this be done?
Yes, it's possible but a little bit ugly. The problem for you is that Docker is very restrictive when it comes to its build context. I'm not sure how familiar you are already with that concept, so here is the introduction from the documentation:
The docker build command builds an image from a Dockerfile and a context.
For example, docker build . uses . as its build context, and since it's not specified otherwise, ./Dockerfile as the Dockerfile. Files or paths outside the build context cannot be referenced in the Dockerfile (so no COPY ..).
The issue for you is that during a Docker build, the build context cannot be left. If you have multiple applications that you want to build, you would normally add a Dockerfile for each app.
src/
├── apps
│ ├── my_app
│ │ └── Dockerfile
│ └── my_other_app
│ └── Dockerfile
└── shared
└── shared_module
Naturally, you would cd into my_app and use docker build . to build the application's Docker image. The issue with this is that you can't access ../../shared from the build, since it's outside of the context.
So you need to make sure both apps and shared is in the build context. One way would be to place all Dockerfile in src like so:
src/
├── Dockerfile.my_app
├── Dockerfile.my_other
├── apps
│ ├── my_app
│ └── my_other_app
└── shared
└── shared_module
You can then build the applications by explicitly specifying the context and the Dockerfile:
src$ docker build -f Dockerfile.my_app .
Alternatively, you can keep the Dockerfiles inside my_app and my_other_app, and point to them:
src$ docker build -f apps/my_app/Dockerfile .
That should also work. In both cases, the build is executed from within src, which means you need to pay a little attention to the paths in the Dockerfile. The working directory is still src:
COPY ./apps/my_app /src/apps/my_app
By mirroring the folder structure you have locally, you should be able to make your dependencies work without any changes:
RUN mkdir -p /src
COPY ./shared /src/shared
COPY ./apps/my_app /src/apps/my_app
RUN cd /src/apps/my_app && npm install
Hope that helps you get started.

How does the #shopify/polaris project get built from github repo to npm dependency?

I'm interested in learning how the #shopify/polaris project in github is built and published to npm. My main questions are:
How are the index.es.js and index.js files being generated? Are those being generated programmatically on my computer, or are they published to npm like this?
What mechanism is preventing the files in the github repo from being downloaded when installed? I don't see a .npmignore in the repo.
Below I have the files in the npm package, and the github, and you can see they're different.
Here's what the polaris project looks like when it's installed via NPM / yarn.
.
├── CHANGELOG.md
├── README.md
├── index.es.js
├── index.js
├── package.json
├── src
├── styles
├── styles.css
├── styles.scss
└── types
Here's what the project looks like on github.
.
├── CHANGELOG.md
├── README.md
├── circle.yml
├── config
├── documentation
├── examples
├── package.json
├── scripts
├── src
├── tests
├── tsconfig.json
├── tslint.json
└── yarn.lock
We use Rollup to generate the different entry files for Polaris. You can see our generic config file here: https://github.com/Shopify/polaris/blob/master/config/rollup/index.js. Note that it does all the work of compiling our TypeScript source files (using TypeScript and Babel), as well as kicking off the work required to bundle our CSS and icons as appropriate. This config generator is then run three times, which you can see here: https://github.com/Shopify/polaris/blob/master/scripts/build.js#L36-L38
The main entry file (.js, and spits out the CSS for our CDN)
The module entry file (.es.js)
The embedded entry file
We then copy these into their final locations for the NPM publish.
As for how we control what files are in the NPM package, we use the files key in our package.json; these are the relevant lines.

How to organize a large single repo node.js app

I have a node.js app using a single git repo, that has various packages. I am trying to make things modularized, and separate.
├── common
│   └── formattr
│   ├── node_modules
│   ├── package.json
│ └── formattr.js
└── providers
├── aws
│   ├── node_modules
│   └── package.json
└── google
├── node_modules
└── package.json
The problem I am running into is that when somebody clones the app, they have to recurse into each package direction and manually run npm install. Additionally, there is lots of wasted resources and duplication, since the package aws might use npm install request but so does the google package. Right now, they both store independ versions.
Finally, there is this problem as well:
// Inside of the aws package, I need formattr. This makes me cry inside.
let format = require('../../common/formattr/formattr');
What is the recommended pattern and structure?

Express app will not start with node app.js

I was following along to this tutorial about setting up an express app. But when I ran node app.js the server would not start. The previous command that I ran was
cd express_example && npm install
But I got this error
npm WARN engine express#4.9.8: wanted: {"node":">= 0.10.0"} (current: {"node":"0.8.4","npm":"2.1.4"})
npm WARN engine void-elements#1.0.0: wanted: {"node":">=0.10.0"} (current: {"node":"0.8.4","npm":"2.1.4"})
So i thought that it was because a newer version of node was necessary. So I looked at the version of node that I had, noticed it was older so I tried upgrading. But when I ran brew update node i got this alert.
Error: node-0.10.32 already installed
I did some searching around and found this answer here on SO. Which suggested running these commands
sudo npm cache clean -f
then
sudo npm install -g n
then
sudo n stable
But the server still wont startup. But at least I am now at v0.10.33
I also found this similar question on SO whose accepted answer said to try sudo node app.js but that just does not seem right, nor did it even work.
If you are using Express 4 and using the generator, there is a slightly diffrent method for running the app. Express 4 changed the file structure to:
├── app.js
├── bin
│ └── www
├── package.json
├── public
│ ├── images
│ ├── javascripts
│ └── stylesheets
│ └── style.css
├── routes
│ ├── index.js
│ └── users.js
└── views
├── error.jade
├── index.jade
└── layout.jade
Notice the bin/www directory.
The app start logic is now seperated in the bin/www file. Therefore you have to run your app with the following command:
DEBUG=<<app_name>> ./bin/www
Reference: http://expressjs.com/starter/generator.html

Resources