local debug nodejs aws lambda - node.js

I am creating a es6 nodejs lambda using serverless npm package. For using js import feature I added type module in package.json and all my js files are using extension .mjs. Now I am trying to deploy my lambdas in my local using serverless offline, when I execute command
serverless offline -s dev
It seems to deploy my functions and my endpoint. However when I hit that endpoint it shows me that can not find handler module. Specifically it can not find the module define in handler for this endpoint
Cannot find module '/home/leo/Documents/User/technical/core/core-freight-logistics/lambda-js/getlabelbyuser-lambda/handler
This is my serverless.yml
service: label-lambda-inter
frameworkVersion: '3'
provider:
name: aws
runtime: nodejs16.x
plugins:
- serverless-offline
functions:
getLabel:
name: get-label
handler: label-lambda-inter/handler.getLabel
environment: ${file(core.${opt:stage, 'dev'}.json)}
executeQuery:
name: execute-query
handler: execute-query-lambda/handler.executeQuery
environment: ${file(core.${opt:stage, 'dev'}.json)}
getLabelByUserId:
handler: getlabelbyuser-lambda/handler.getLabelByUserId
environment: ${file(core.${opt:stage, 'dev'}.json)}
events:
- http:
path: inter-label/{id}
method: get
cors: true
This is my package.json:
{
"name": "lambda",
"type": "module",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "./node_modules/.bin/serverless offline -s dev",
"debug": "export SLS_DEBUG=* && node --inspect /usr/local/bin/serverless offline -s dev"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.1165.0",
"dotenv": "^16.0.1",
"pg-promise": "^10.11.1",
"uuid": "^8.3.2"
},
"devDependencies": {
"serverless-offline": "^8.8.0"
}
}
I am not sure if this is caused by the extension mjs of my handler file. Can someone help me please?
Thanks

Related

Node js + Vercel : Not able to host index.mjs on Vercel (500 internal server error)

I am creating an API service using Node.js and wish to deploy it on vercel. The application works perfectly fine on my localhost but gives a 500 error post deploying on vercel. It is because I have an mjs extension for my index.mjs. I am also using a package that needs commonjs to run.
Below is my directory structure
Below is my package.json
{
"name": "apis",
"version": "1.0.0",
"description": "",
"main": "index.mjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1" },
"type": "commonjs",
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#nomiclabs/hardhat-ethers": "^2.2.0",
"#nomiclabs/hardhat-waffle": "^2.0.3",
"#openzeppelin/contracts": "^4.7.3",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"hardhat": "^2.12.0",
"multer": "^1.4.5-lts.1",
"nft.storage": "^7.0.0",
"web3": "^1.8.0"
},
"devDependencies": {
"nodemon": "^2.0.20"
}
}
and my vercel.json
{
"version": 2,
"builds": [
{
"src": "index.mjs",
"use": "#now/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "index.mjs"
}
]
}
My setup was hosting perfectly with the same configuration when I was using index.js instead of index.mjs. However, after adding functionalities I cannot avoid mjs.
Below is the error
Server logs
[GET] /
20:41:35:24
2022-11-03T15:11:36.500Z undefined ERROR Error [ERR_REQUIRE_ESM]: require() of ES Module /var/task/index.mjs not supported.
Instead change the require of /var/task/index.mjs to a dynamic import() which is available in all CommonJS modules.
at /var/task/___now_launcher.js:26:28
at Object.<anonymous> (/var/task/___now_launcher.js:74:7)
at _tryRequireFile (file:///var/runtime/index.mjs:869:37)
at _tryRequire (file:///var/runtime/index.mjs:919:25)
at _loadUserApp (file:///var/runtime/index.mjs:945:22)
at Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:976:27)
at start (file:///var/runtime/index.mjs:1137:42)
at file:///var/runtime/index.mjs:1143:7
at async Promise.all (index 0) {
code: 'ERR_REQUIRE_ESM'
}
Please help
Answering my own question.
It seems vercel has problems hosting ES6 modules. I was able to host this with heroku

Unable to debug Node.js application in WebStorm using Serverless

I am currently creating a lambda function in Node.js and want to debug the code locally. I am using WebStorm as my IDE.
In my serverless.yml
service: applicationsync
frameworkVersion: '3'
provider:
name: aws
runtime: nodejs12.x
plugins:
- serverless-offline
functions:
hello:
handler: index.handler
events:
- http:
path: hello
method: get
- http:
path: hello
in my package.json
{
"name": "applicationsync",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "./node_modules/.bin/serverless offline -s dev",
"debug": "set SLS_DEBUG=* && node --inspect /usr/lib/node_modules/serverless/bin/serverless offline -s dev"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"serverless-offline": "^8.8.0"
},
"dependencies": {
"aws-sdk": "^2.1170.0",
"mysql": "^2.18.1"
}
}
And then I have added some screen shots.
I click on debug option:
The following runs:
My file structure:
When I send a request I get a response back but break points are ignored and debug is not triggered.
I am wondering what I am missing.

Compile typescript with corresponding dependencies to upload to AWS Lambda

I want to create a service with typescript and upload it to AWS Lambda. When I have it and I run npm run compile it creates the corresponding javascript files, but when I zip them and upload them to AWS it does not recognize the configured dependencies that are in package.json, and are imported inside the code. What is the best way to do this in an optimal way? Of course I want to compile the modules that are marked as dependencies, and not the devDependencies.
This is my package.json:
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"aws-sdk": "^2.673.0",
"tslog": "^3.3.1"
},
"name": "project-name",
"description": "",
"version": "1.0.0-SNAPSHOT",
"main": "index.js",
"devDependencies": {
"#types/aws-lambda": "^8.10.53",
"#typescript-eslint/eslint-plugin": "^5.8.1",
"#typescript-eslint/parser": "^5.8.1",
"eslint": "^8.5.0",
"typescript": "^3.9.5"
},
"scripts": {
"test": "jest",
"compile": "tsc",
"lint": "eslint . --ext .ts"
},
"author": "",
"license": "ISC"
}
This is the handler:
import {Callback, Context} from "aws-lambda";
import {Logger} from "tslog";
export class Handler{
public handleRequest(event: any, context: Context, callback: Callback){
const log: Logger = new Logger()
log.debug(event)
}
}
And when I run this code in an AWS Lambda it throws:
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'tslog'\nRequire stack:\n- /var/task/build/src/Handler.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js"

Github Action - Error: Process completed with exit code

I am trying to setup Github actions to npm publish my package. But I got this error When I move on execute
My workflows/publish.yml file looks like the following:
name: publish
on:
push:
branches: [ main ]
jobs:
release:
name: publish
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout#v2.3.4
- name: Setup Node.js environment
uses: actions/setup-node#v2.2.0
with:
node-version: 14
registry-url : https://registry.nmpjs.org
- name: publish
run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.NODE_AUTH_TOKEN}}
Your package.json file is broken.
{
"name": "#sakibb019/npx-card",
"version": "1.0.0",
"description": "",
"main": "card.js",
"scripts": {
"dev": "nodemon card.js"
},
"keywords": [],
"author": "",
"repository": {
"url": "git://github.com/sakibb019/test.git"
},
"license": "ISC",
"dependencies": {
"boxen": "^5.0.1",
"chalk": "^4.1.1",
"clear": "^0.1.0",
"inquirer": "^8.1.0"
}
}
I've added the missing braces, still, you would need to find a complete package JSON file. There should be more content after "inquirer": "^8.1.

Google AppEngine launch the application every 10 minutes

I have a nodejs app deployed on App Engine, the problem i am facing is that the app Engine is hitting command 'node app.js' repeatedly after 10 minutes. I have only one instance of app at a time and currently serving only one version.
Here is the package.json and app.yaml in which app is configured
package.json
{
"name": "appengine-Relay-dashboard",
"description": "Manages the cronjobs in which the cloud functions are emitted.",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": "9.x"
},
"scripts": {
"deploy": "gcloud app deploy",
"start":"node app.js",
"lint": "repo-tools lint",
"pretest": "npm run lint",
"system-test": "repo-tools test app",
"test": "npm run system-test",
"e2e-test": "repo-tools test deploy"
},
"dependencies": {
"#google-cloud/bigquery": "^1.2.0",
"aws-sdk": "^2.230.1",
"cron": "^1.3.0",
"express": "^4.16.2",
"moment-timezone": "^0.5.16",
"request": "^2.85.0",
"request-promise": "^4.2.2",
"sleep": "^5.1.1",
"twilio": "^3.16.0"
},
"devDependencies": {
"#google-cloud/nodejs-repo-tools": "2.2.1"
},
"cloud-repo-tools": {
"test": {
"app": {
"msg": "Hello, world!"
}
},
"requiresKeyFile": true,
"requiresProjectId": true
}
}
app.yaml
runtime: nodejs
env: flex
manual_scaling:
instances: 1
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
Here is the problem in the image i am facing
Does anyone know how to fix this?
The actual problem was the url in the app engine was returning 502 error!
So i made a get request(app.get('/')) to the url and when it started working, it stopped repeating itself. I got the solution myself.
The main reason was the url was not working at all. So it stopped hitting node app.js once i added the get request to the url and redeployed the app again!

Resources