Nodejs modularized aws-sdk v3 size getting increased - node.js

I am trying to reduce size of nodejs lambda bundle which uses aws-sdk.
This is the original lambda package.json file:
{
"name": "lambdanodejs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.784.0",
"bluebird": "^3.7.2",
"ioredis": "^4.19.2",
"redis": "^3.0.2",
"redis-clustr": "^1.7.0"
}
}
Overall size is 57MB, 54 belongs to aws-sdk.
To reduce size I tried using specific client services (v3 sdk).
Followed : https://github.com/aws/aws-sdk-js-v3/blob/main/clients/client-secrets-manager/package.json
{
"name": "lambdanodejs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"#aws-sdk/client-dynamodb": "^3.7.0",
"#aws-sdk/client-secrets-manager": "^3.7.0",
"bluebird": "^3.7.2",
"ioredis": "^4.19.2",
"redis": "^3.0.2",
"redis-clustr": "^1.7.0"
}
}
Now npm install results in even larger size around 190+MBs.
Also in node_modules I see lot of directories which were not there when using previous package.json install.
This v3 aws-sdk is supposed to be lighter.
Am I missing something?
Thanks!

Don't push aws-sdk to the lambda function. If you want to use it in the local development environment install it globally.
Lambda will provide all necessary SDK modules(v2 & v3) during runtime, just import them in the code.
Don't bundle the aws-sdk in the zip.
If your zip size is getting increased, use layers in lambda.
Import the node_moudles to lambda layers.
Then upload your code to lambda.

Related

"/" is not recognized as an internal or external command

Today, I'm coding a few npm packages and a few things that need to be prepared repeatedly.
So I wanted to code a CLI to get those things done quickly.
Here is the src/cli.js code:
export function cli(args){
console.log(args);
}
Here is the package.json code:
{
"name": "my-project",
"version": "1.0.0",
"description": "A CLI to bootstrap new project",
"main": "src/index.js",
"bin": {
"#kensoni/my-project": "bin/my-project",
"my-project": "bin/my-project"
},
"publishConfig": {
"access": "public"
},
"keywords": [
"cli"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Ken Nguyen",
"license": "MIT",
"dependencies": {
"arg": "^5.0.0",
"esm": "^3.2.25",
"inquirer": "^8.1.1"
}
}
Here is the bin/my-project code:
#!/usr/bin/env/ node
require = require('esm')(module /*, options*/);
require('../src/cli').cli(process.argv);
After I execute the command npm link and open a new cmd type my-project, I get the following message:
'"/"' is not recognized as an internal or external command,
operable program or batch file.
I am using these versions:
node: 14.17.1
npm: 7.18.1
Any ideas how it might work.
Thanks in advance.
Remove "/" after env
#!/usr/bin/env node
//...

Node modules are not exporting

my node modules are not importing. I tried in export default and export const and each and every way. every each times I get either reference error or a syntax error.
I get reference error when i update my code like below.(ReferenceError: require is not defined)
const viewEngine = require( './config/viewEngine');
I get syntax error when i update my code like below.(SyntaxError: The requested module './config/viewEngine' does not provide an export named 'default')
import viewEngine from './config/viewEngine';
I get syntax error when i update my code like below.(SyntaxError: The requested module './config/viewEngine' does not provide an export named 'viewEngine')
import {viewEngine} from './config/viewEngine';
and the ways i tried to export:
module.exports = configViewEngine;
module.exports = {configViewEngine};
and my dotenv is not getting defined. I tried every each ways to import dotenv too.
here is my package.json.
{
"name": "chatbot",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon --experimental-modules --es-module-specifier-resolution=node src/server.js"
},
"type": "module",
"author": "",
"license": "ISC",
"dependencies": {
"#babel/core": "^7.14.3",
"#babel/node": "^7.14.2",
"#babel/preset-env": "^7.14.2",
"body-parser": "^1.19.0",
"browserify": "^17.0.0",
"dotenv": "^10.0.0",
"ejs": "^3.1.6",
"express": "^4.17.1",
"localtunnel": "^2.0.1",
"nodemon": "^2.0.7",
"npm-upgrade": "^3.0.0"
}
}
I use windows 10.
please help thanks!
After upgrading npm version it worked.

For some reason, electron-packager is not able to find electron... Any idea what to do?

For some reason, electron-packager can't find the module electron. I have installed it as instructed from the official site, yet it doesn't seem to work. Here is what seems to happen, my package.json, and how I run electron-packager
Package.json
{
"name": "electron-part1",
"version": "1.0.0",
"description": "An electron tutorial",
"main": "index.js",
"jquery": "^3.3.1",
"scripts": {
"start": "electron .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Me",
"license": "ISC",
"devDependencies": {
"electron": "^5.0.3",
"electron-packager": "^8.7.2"
},
"dependencies": {
"custom-electron-titlebar": "^3.2.5",
"jquery": "^3.5.1"
}
}
What happens:
How I call electron-packager:
electron-packager . electron-part1 --platform=win32 --arch=x64
I fixed it! If anyone else is having the same problem, copy electron from %USERPROFILE%\AppData\Roaming\npm\node_modules, and paste it to node modules in your main folder (The one with package.json) then you can package it as normal

When trying to create a release pipeline in Azure DevOps (I have used a Nodejs application) it exits with an error. ##[error]Bash exited with code '1'

NodeJs application that I have been trying to deploy, always shows an error:
[error]Bash exited with code '1'.
{
"name": "test1",
"version": "1.0.0",
"main": "app.js",
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js",
"build": " "
},
"keywords": ["app.js"],
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"connect-flash": "^0.1.1",
"ejs": "^2.6.2",
"express": "^4.17.1",
"express-ejs-layouts": "^2.5.0",
"express-session": "^1.16.2",
"i": "^0.3.6",
"mongoose": "^5.6.5",
"passport": "^0.4.0",
"passport-local": "^1.0.0"
},
"devDependencies": {
"nodemon": "^1.19.1"
},
"description": ""
}
First, Please check the path in the artifacts weather the files that are required exists in the artifacts or not. Download the artifact that you are trying to deploy and make sure the package.json and app.js is in the root directory (or in the deirectory that you are trying to deploy).
For further diagnoisis i think you need to provide your question with more detail descriptions like
What build script was used to make the artifact.
What environment was used for app deployment.

How to start my globally installed node application?

I developed small application.
I did this to install developed module globally:
npm install . -g
I got this response:
$ npm install -g .
update-deps-dev#1.0.0 C:\Users\Vladislav.Sharikov\AppData\Roaming\npm\node_modul
es\update-deps-dev
I got my module in place, where other globally installed modules are stored. Also, my module is available by:
npm ls -g --depth=0
Now, I want to start this module from any place on my pc. How should I do this?
I try to run, but getting this:
Vladislav.Sharikov#PC /D/Dev
$ update-deps-dev
sh: update-deps-dev: command not found
I am using Windows 7 x64 + Git Bash.
How should I start my globally installed node application?
My package.json file contents:
{
"name": "update-deps-dev",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"adm-zip": "^0.4.7",
"fs": "0.0.2",
"https": "^1.0.0",
"q": "^1.4.1",
"shelljs": "^0.7.0"
}
}
Not sure about git bash. But in a cross platform way you can simply define a bin field on your package json to tell npm to generate a binary available on your command line later. It really works fine on windows, mac, linux.
Note that it is a per user binary. If you are working on linux it won t install it into /usr/bin, but depending on your system, more probably ~/node_modules/bin.
Suppose your binary is defined into bin.js file, you just need to add a new Object field bin, and foreach bin you want to define a key, the name of the binary, and its value must point to the path of your bin file relative to the package file:
{
"name": "update-deps-dev",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {
"update-deps-dev": "./bin.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"adm-zip": "^0.4.7",
"fs": "0.0.2",
"https": "^1.0.0",
"q": "^1.4.1",
"shelljs": "^0.7.0"
}
}
Read also,
https://docs.npmjs.com/files/package.json#bin
https://docs.npmjs.com/cli/bin

Resources