Install bcrypt on AWS Elastic Beanstalk failed - amazon-web-services

Look like installation of the bcrypt failed since I can see in the log message Cannot find module 'bcrypt', that is mean it was not installed.
I was add .npmrc file with unsafe-perm=true and add 00_change_npm_permissions.config with the following content:
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/00_set_tmp_permissions.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
chown -R nodejs:nodejs /tmp/.npm
But none of the solutions are working. Any suggestions?

I was able to fix it by modifying a little bit scripts in package.json this way:
"scripts": {
"build": "tsc",
"dev": "ts-node ./src/server.ts",
"bcrypt": "npm install bcrypt",
"start": "npm run bcrypt && node server.js",
"prod": "npm run build && npm run start"
},

Related

How to only install production dependencies and then some, but without any development dependencies?

Given the following Dockerfile and package.json, how do I get my image to only have what is defined as production dependencies in the package.json and the one additional dependency defined in the RUN --no-save layer, but not any of the development dependencies?
FROM node:16.14.2-alpine3.15
WORKDIR /opt/asd
COPY ./package.json .
RUN npm install --production
RUN npm install --no-save typescript
{
"devDependencies": {
"express": "4.17.3"
},
"dependencies": {
"ramda": "0.28.0"
}
}
Running docker build . produces an image that has node_modules containing not only the deps I wanted but also the development dependencies. What do I need to change in the Dockerfile to avoid this?
You can use bash conditionals syntax, but you also need to define $NODE_ENV as environment variable:
RUN if [ "$NODE_ENV" = "production" ]; \
then npm install --only=production; \
else npm install; \
fi

Deploying NestJS application on Elastic Beanstalk

I am trying to deploy my NestJS application to AWS elastic beanstalk, but did not had any success, can someone please write step by step how can I achieve that?
Full explanation:
I have a nestjs app with typeorm but didn’t configured it to work with RDS, so we will leave it for now (maybe there is a connection, idk).
First of all I made a CodePipline that when I am pushing new version to my github repo it automatically deploying the whole repo to my eb instance that works on node 12.x.
Now, I want that on every git push, the instance will install the dependencies, build the nest app, and start the server from the /dist/main.js.
I have added a Procfile with:
web: npm install && npm run build && npm run start:prod
I have also added PORT environment variable on EB that configured on main.ts and when not found to use 8080.
And my package.json scripts are like a newly created nest app:
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
}
When deploying I see in the logs that it did something like this:
Jan 23 20:01:14 web: > app#0.0.1 start /var/app/current
Jan 23 20:01:14 ip-172-31-17-171 web: > sudo npm i -g #nestjs/cli && node dist/main
Jan 23 20:01:14 web: We trust you have received the usual lecture from the local System
Jan 23 20:01:14 web: Administrator. It usually boils down to these three things:
Jan 23 20:01:14 web: #1) Respect the privacy of others.
Jan 23 20:01:14 web: #2) Think before you type.
Jan 23 20:01:14 web: #3) With great power comes great responsibility.
Jan 23 20:01:14 web: sudo: no tty present and no askpass program specified
Jan 23 20:01:14 web: npm ERR! code ELIFECYCLE
Jan 23 20:01:14 web: npm ERR! errno 1
Jan 23 20:01:14 web: npm ERR! app#0.0.1 start: `sudo npm i -g #nestjs/cli && node dist/main`
Jan 23 20:01:14 web: npm ERR! Exit status 1
Jan 23 20:01:14 web: npm ERR!
Jan 23 20:01:14 web: npm ERR! Failed at the app#0.0.1 start script.
So I am getting 502 when entering the env url.
So maybe it is a permission issue with npm? Or I need to deploy on some way only the /dist folder?
What do you think the problem is?
It is my first time trying to deploy a backend server to eb :)
I got stuck with the deploying the nestjs app to AWS elastic beanstalk for a while so that I decide make the guide here. I make the deploy guide step by step here for deploying the simple app.
The reason WHY the deploying is usually FAILED because it does not know NEST dependencies (it does not install dev dependencies) and Typescript code which need to compile before uploading to server.
In my case, Use the compression for making the zip file (you can use the command line eb but try to use this first for simple).
Firstly, Preparing the zip file, this is really IMPORTANT for avoiding the FAILED while it is deploying.
You need to make dist folder. For doing this, we remove the dist folder first and run the command nest build (* this one do the tsc for compile Typescript for us).
Now, we have the dist folder, the next thing we need to do is copying the package.json ( * this one installing the DEPENDENCIES for us ) into dist folder to let server know installing the dependencies that we do not bring the node_module here.
In package.json, yarn start to run nest start but the AWS does not know Nest command so that we need to point it out by using node main.js (this file is in dist file, you need to make the path to main.js correctly). For doing this, we create the Procfile (reference this) and add the conntent is web: node src/main.js. Remember copying it into the dist file.
Compressing file correctly as this
Secondly, from AWS console creating the application or environment then uploading the zip file and get successful status. If not, check the log file to know why it failed. Maybe it does not the nest command, etc...
Hoping this guide will help you.
Cheer!
I was stuck with the following EBS eb-engine.log error:
"Instance deployment: You didn't include a 'package.json' file in your
source bundle. The deployment didn't install a specific Node.js
version."
"Instance deployment failed to generate a 'Procfile' for Node.js.
Provide one of these files: 'package.json', 'server.js', or 'app.js'.
The deployment failed."
Using all previous answers, the following works for me with CodePipeline and EBS:
buildspec.yml
version: 0.2
phases:
install:
commands:
- npm install
build:
commands:
- npm run build
post_build:
commands:
- cp -R node_modules/ dist/node_modules # nodejs needs this
- cp Procfile dist/Procfile # EBS needs this
artifacts:
files:
- "**/*"
discard-paths: no
base-directory: dist
Procfile
web: node main.js
The way it works is that CodeBuild handles Nest building, and CodeDeploy only deploys what's in dist/
Node will need node_modules so post build I copy it to dist/
But EBS complains there's no package.json or any file it recognizes in dist/, and since Procfile is usually at root, copy it so EBS finds it and starts by using node main.js
"build": "nest build && cp package.json dist/",
"postbuild": "cd dist && zip ../dist.zip -r * .[^.]*",
"start": "node main.js"
Modifying just these three scripts, it works for me. Procfile is not required.
Finally in the root folder of the project, the dist.zip file that was generated I upload it to Amazon
Change your artifacts to copy everything, so that Codebuild can copy your node_modules, package.json, dist folder automatically.
artifacts:
files:
- "**/*"
Then in your package.json, change the start command to production by default -
"start": "node dist/main"
Here is my buildspec.yml and package.json which works fine on Nestjs deployment on Elastic beanstalk
The mysterious part is this: where is sudo npm i -g #nestjs/cli coming from?
I'm also deploying to Elastic Beanstalk but I only have web: npm run start:prod in my Procfile. The build happens in CI, for that I'm using GitHub Actions. The general outline of the workflow file is as follows:
Checkout
Setup Node
Install dependencies and build app
Generate deployment ZIP archive
Upload deployment archive to Elastic Beanstalk
I suggest you make your Procfile be like mine, run the build script locally, generate the deployment archive using something like zip -r deployment.zip dist package* Procfile and upload it to Elastic Beanstalk and see what the outcome is.

AWS Amplify Build Settings

Using Amplify I'm having difficulty deploying a React application which I believe is due to the build settings.
When trying to deploy the default build settings provided are shown below:
I know this is incorrect and the error I find in the build logs is:
2020-05-14T00:02:22.327Z [WARNING]: !! No index.html detected in deploy folder: /codebuild/output/src568504829/src/chatterfield/
The deploy is successful except when I launch the application I receive an ERR_TOO_MANY_REDIRECTS. After I changed the baseDirectory in build settings to /client/public to point to index.html. The app appears to launch without the REDIRECT error, but nothing loads. I'm guessing this is because I am not running an npm run build command, or not loading a prebuild command. Any help would be greatly appreciated. Thank you
Here is the repo this app is linked to:
https://github.com/travelerr/chatterfield
Change baseDirectory:/ to baseDirectory:build . It worked for me.
For anyone who finds this and put their app in a folder called frontend (or anything else substitute frontend for the path to your app.
version: 0.1
frontend:
phases:
preBuild:
commands:
- cd frontend
- npm ci
build:
commands:
- npm run build
artifacts:
baseDirectory: ./frontend/build
files:
- '**/*'
cache:
paths:
- node_modules/**/*
If your react folder name is "mycoolproject", your yml will need to cd into that directory. Then run the build command. baseDirectory should be where your your final build resides.
version: 0.1
frontend:
phases:
preBuild:
commands:
- cd mycoolproject
- npm ci
build:
commands:
- npm run build
artifacts:
baseDirectory: ./mycoolproject/build
files:
- '**/*'
cache:
paths:
- node_modules/**/*
Also, your amplify should point to a build directory... mycoolproject/amplify/config/project-config.json
{
"providers": [
"awscloudformation"
],
"projectName": "mycoolproject",
"version": "3.1",
"frontend": "javascript",
"javascript": {
"framework": "react",
"config": {
"SourceDir": "src",
"DistributionDir": "build",
"BuildCommand": "npm run-script build",
"StartCommand": "npm run-script start"
}
}
}

How to modify a deployed create-react-app on Github Pages?

I deployed a create-react-app on GitHub Pages using a mix of these 2 tutorials (https://create-react-app.dev/docs/deployment & https://github.com/gitname/react-gh-pages). It's working just fine.
But this webpage is supposed to evolve regularly, I have some content to add.
Is there a way to modify the deployed app? How can I push the changes I would make in my local directory to gh-pages (production mode)? Do I need to run npm build again after my additions?
For the moment I have the development mode on master branch. I think it's the build mode on gh-pages, although I don't have any other branch.
Thank you for your help.
What I did to deploy:
Terminal
$ npm install --save gh-pages
$ git init
$ git remote add origin https://github.com/REMOTE
$ npm run deploy
Package.json
{
"homepage": "https://USER-PAGE.github.io/",
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -b master -d build",
},
"devDependencies": {
"gh-pages": "^2.1.1"
}
}
To modify the site just make the changes and then git add, commit and push like one would normally do to the required branch.
Then again run the command:
npm run deploy

Start up script to re-start the NodeJS app is not working

I'm trying to use the startup-script meta to automatically start my server when my instance resets.
The startup script runs, but the actual command I need doesn't work. What am I doing wrong in this script?
#! /bin/bash
echo "testing" > /tmp/test-script.txt # This WORKS
cd /../ && npm start # This does NOT work
If you look in your package.json file, it says "start": "command" and that is the command that is run when you use the npm start command. The command should be changed in package.json: "start": "node ./pathToStartUpScripts". For example in an Express.js project the start command would be "node ./bin/www" as that is where all the application start up scripts are written.