NodeJS API deployement on AWS with Elastic Beanstalk - node.js

I'm looking for deploying my NodeJS API with AWS.
I tried to use Elastic Beanstalk but I always get this issue:
Image of the error I get
There is my package.json
{
"name": "",
"scripts": {
"start": "node server.js"
},
"version": "0.0.0",
"private": true,
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.3",
"express": "^4.16.4",
"hammerjs": "^2.0.8",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.4.20",
"mongoose-unique-validator": "^2.0.2",
"multer": "^1.4.1",
"tslib": "^1.9.0"
}
}
And I also have a nodecommand.config into a folder named ".ebextensions"
option_settings:
aws:elasticbeanstalk:container:nodejs:
NodeCommand: "npm start"
Also, is Elastic Beanstalk the best way to deploy my API ?
Thx for the answers !

Based on the comments.
The primary issue was using rar for deployment packaged instead of zip. After fixing that, config files had to be adjusted to work with Amazon Liunx 2.

Related

Are heroku servers slow?

I have a node js webiste, when I run it on localhost on my computer, it runs blazingly fast but when I deploy it on heroku, it becomes very slow. So will this also happen if I host my site with digital ocean or any other hosting provider?
This is my package.json
{
"name": "something",
"version": "1.0.0",
"engines": {
"node": "16.x"
},
"description": "",
"main": "app.js",
"scripts": {
"start": "cross-env NODE_ENV=production node app",
"dev": "cross-env NODE_ENV=development nodemon app"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"compression": "^1.7.4",
"connect-mongo": "^4.6.0",
"cross-env": "^7.0.3",
"cryptr": "^6.0.3",
"dotenv": "^16.0.0",
"express": "^4.17.3",
"express-handlebars": "^6.0.5",
"express-session": "^1.17.2",
"flatted": "^3.2.5",
"got": "^12.1.0",
"lodash": "^4.17.21",
"method-override": "^3.0.0",
"moment": "^2.29.3",
"mongoose": "^6.3.1",
"most-common-words-by-language": "^3.0.10",
"node-fetch": "^3.2.4",
"passport": "^0.5.2",
"passport-google-oauth20": "^2.0.0",
"randomcolor": "^0.6.2",
"request": "^2.88.2",
"workbox-cli": "^6.5.3"
}
}
It is most likely not a hosting provider issue but rather a limitation issue.
Heroku uses the Linux containers Dynos to execute your code in their platform, and they have multiple types with differentiating characteristics. If you have an unverified account, then you're given the free dyno type by default. Check Heroku's dynos page. You'll see that for the "Always on" row, the free type has the trait:
"Sleeps after 30 mins of inactivity, otherwise always on depending on your remaining monthly free dyno hours."
This is your issue. Your app starts slower when the dynos are asleep. The speed improves as you continue using the app, but it's not going to be performant and it's going to start slow again once the app is inactive for 30 mins. If you want your app to be blazingly fast in Heroku, then you need to verify your account and purchase a non free or hobby dyno type.
Take note that you will also face similar issues with other hosting providers if you are using free account on their platform.

NPM version discrepancy between main version and cpanel version

When running npm-version on my local server/laptop I receive 7.5.6
However, when running on cpanel terminal -- I receive
[~]# /opt/cpanel/ea-nodejs10/bin/npm --version
6.14.11
And this 6.14.11 is after I ran
[~]# /opt/cpanel/ea-nodejs10/bin/npm install --latest version -g
Which I thought would update it to the 7.5.6 as I used the same command to update the one on my computer as well.
In all I'm having trouble deploying my webapp on bluehost via cpanel. Even though the app is registered with the application manager -- and the Ensure Dependencies part came out done and successful. And the git repo is cloned and deployed with no errors.
Would this discrepancy be causing the app not to launch?
I can't understand why it isn't deploying properly, so I was thinking there may be something with the package.json or (package-lock.json) -- ei - could the versions in such bet preventing deployment due to discrepancies.
Would love some help, been trying to get this deployed for days!
my package.json
{
"name": "vandenbergdevelopment",
"version": "1.0.0",
"description": "DeltaDesignServer",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js"
},
"author": "J Vandenberg",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"ejs": "^3.1.6",
"express": "^4.17.1",
"firebase-tools": "^9.5.0",
"hover.css": "^2.3.2",
"nodemailer": "^6.4.18",
"nodemailer-mailgun-transport": "^2.0.2",
"version": "^0.0.4"
}
}
The top bit to the lock.json
"": {
"name": "vandenbergdevelopment",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"version": "^0.0.4",
"body-parser": "^1.19.0",
"ejs": "^3.1.6",
"express": "^4.17.1",
"firebase-tools": "^9.5.0",
"hover.css": "^2.3.2",
"nodemailer": "^6.4.18",
"nodemailer-mailgun-transport": "^2.0.2"
}
},
Thank you!

Nodejs Express server Cannot find module 'pdf-fill-form'

I'm really new to NodeJS and ExpressJS.
This is nodeJS RestAPI service.
In my project, I have to populate a PDF form field, To do that I wanted to get help from a nodejs library "pdf-fill-form" link ->https://www.npmjs.com/package/pdf-fill-form
I ran 'npm install pdf-fill-form' on my project folder terminal
Import it as in the example -> const pdfFillForm = require('pdf-fill-form');
But it throws an error
internal/modules/cjs/loader.js:796
throw err;
^
Error: Cannot find module 'pdf-fill-form'
I have tried multiple libraries same issue came 'Cannot find module "Lib Name"'
Can someone who is an expert on Node JS help me?
Probably I have done something wrong, I did search for solutions but could not find any.
My Package json
{
"name": "pemsbackend",
"version": "1.0.0",
"description": "TB",
"main": "index.js",
"scripts": {
"start": "nodemon index.js"
},
"author": "MyTest TB",
"license": "ISC",
"dependencies": {
"#hapi/joi": "^17.1.0",
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.9.2"
},
"devDependencies": {
"nodemon": "^2.0.2"
}
}
Thank you.
pdf-fill-form has other dependencies to be installed in you're system before installing it. Go through installing guide mentioned int it's repo and then try installing it.

Cannot upgrade Node.js version in Azure App Service

My Azure app service is running Node.js version 0.10:
> node -v
D:\home\site\wwwroot
v0.10.28
I am trying to upgrade it to version 8.
There's an article here describing how to do that: https://learn.microsoft.com/en-us/azure/nodejs-specify-node-version-azure-apps
First, I added the key in the Application Settings in Azure Portal:
It didn't work, so I also changed packages.json:
{
"name": "azure_cosmos_db_webservice",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node --inspect server.js"
},
"engines":{"node": "8.x"}, <-- This line added by me
"dependencies": {
"async": "^2.1.2",
"body-parser": "~1.15.2",
"cookie-parser": "~1.4.3",
"debug": "~2.2.0",
"documentdb": "^1.10.0",
"dotenv": "^4.0.0",
"express": "~4.14.0",
"morgan": "~1.7.0",
"serve-favicon": "~2.3.0"
}
}
I've restarted the app service several times by stopping and starting it again.
However, node -v in the console in Azure Portal is still showing the version as 0.10.28.
What did I miss?
Turns out that 8.x was not a correct syntax.
I've changed it to 8.1.4 both in packages.json and in the Application Settings, and it worked.
I found the list of supported versions here:
https://MYSITE.scm.azurewebsites.net/api/diagnostics/runtime

Heroku: Cannot find module 'mongodb'

OK, I have an express app which is throwing this error at launch when deployed to Heroku. The app compiles and runs fine locally:
Cannot find module 'mongodb'
This app was running until I redeployed it yesterday ... here is my package.json which was unchanged
{
"name": "fitness-tracker",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"express": "~4.9.0",
"body-parser": "~1.8.1",
"cookie-parser": "~1.3.3",
"promise": "~6.0.1",
"morgan": "~1.3.0",
"serve-favicon": "~2.1.3",
"debug": "~2.0.0",
"mongodb-core": "*",
"mongoskin": "~2.0.0",
"express-handlebars": "~1.1.0",
"moment": "~2.0.0",
"moment-timezone": "^0.3.1",
"q": "~1.0.1",
"compass": "~0.1.0",
"apm": "*"
},
"devDependencies": {
"moment-timezone": "^0.3.1"
}
Ideas?
So the issue was package's resolution based on node version. Since NodeJS is rapidly evolving, it's a good practice to include engines specification in your package.json.
When doing automatic deployments on various cloud providers, this helps them to use only specified node & npm version. This was all the packages that get fetches are built for that node version hence less chances of app to misbehave.
Read more: https://docs.npmjs.com/files/package.json#engines
Also always worth a try: disable the Heroku build cache to recreate the node_modules directory.
In the Heroku CLI write:
$ heroku config:set NODE_MODULES_CACHE=false
$ git commit -am 'disable node_modules cache' --allow-empty
$ git push heroku master
As described here.

Resources