CDK + SAM LOCAL + AWS-SDK v3 - node.js

I have a cdk project that includes lambda functions, dynamodb and api gateway. To run it locally I use SAM local:
sam local start-api -t ./cdk.out/stackName.template.json
In the project I use aws-sdk v2 and everything works fine.
Now, I want to change to aws-sdk v3 so I install dynamoDB client:
npm install #aws-sdk/client-dynamodb
and import it in my lambda index file:
const { DynamoDBClient } = require("#aws-sdk/client-dynamodb");
https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/index.html
i run cdk synth and then again sam local start-api -t ./cdk.out/stackName.template.json, everything seems fine.
But when I hit the lambda function i get error this error:
undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Unexpected token '?'","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token '?'"," at _loadUserApp (/var/runtime/UserFunction.js:98:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:999:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)"," at Module.load (internal/modules/cjs/loader.js:863:32)"," at Function.Module._load (internal/modules/cjs/loader.js:708:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)"," at internal/main/run_main_module.js:17:47"]}
time="2022-11-24T13:14:55.537" level=error msg="Init failed" InvokeID= error="Runtime exited with error: exit status 129"
My package.json:
{
"name": "aws-api-v1",
"version": "0.1.0",
"scripts": {},
"dependencies": {
"#aws-sdk/client-dynamodb": "^3.216.0",
"#aws-sdk/client-secrets-manager": "^3.216.0",
"jsonwebtoken": "^8.5.1"
}
}
Any suggestion?
Thank you.

Related

electrode-server throwing hapi webpack error

I am trying run this app locally using the below run command, and I am getting errors with starting the electrode server. I am rather new to this so please let me know what information is necessary in order to diagnose my issue. Thank you in advance for any help.
Relevant Dependencies
"#walmart/electrode-ccm-client": "3.3.1",
"#walmart/electrode-quimby-client": "2.0.2",
"#walmart/electrode-ui-config": "3.8.2",
"#walmart/electrode-wml-app": "3.3.5",
"electrode-archetype-react-app": "6.5.4",
"hapi": "^17.0.0",
- Dev -
"#walmart/electrode-build-utils": "^1.1.0",
"electrode-archetype-react-app-dev": "6.5.4",
"webpack-bundle-analyzer": "^3.7.0"
Run command:
NODE_TLS_REJECT_UNAUTHORIZED=0 clap dev
Terminal output:
Starting Electrode webpack dev server
electrode-server caught an error while starting your server
failed registering your plugin 'webpackDevHapi' with module '"./dev-hapi.js"' from path: 'CWD/node_modules/electrode-archetype-react-app-dev/lib/dev-admin'
message: Invalid plugin options {
"__name": "webpackDevHapi",
"module": "./dev-hapi.js",
"requireFromPath": "CWD/node_modules/electrode-archetype-react-app-dev/lib/dev-admin"
}
[1] "attributes" is required
Suggestion to resolve the issue:
Please double check and verify your plugin 'webpackDevHapi'.
If you have followed this resolution step and you are still seeing an
error, please file an issue on the electrode-server repository
git+https://github.com/electrode-io/electrode-server.git
Error: Invalid plugin options {
"__name": "webpackDevHapi",
"module": "./dev-hapi.js",
"requireFromPath": "CWD/node_modules/electrode-archetype-react-app-dev/lib/dev-admin"
}
[1] "attributes" is required
at Object.exports.assert (CWD/node_modules/electrode-server/node_modules/hoek/lib/index.js:740:11)
at Object.exports.apply (CWD/node_modules/electrode-server/node_modules/hapi/lib/schema.js:17:10)
at module.exports.internals.Server.internals.Plugin.register (CWD/node_modules/electrode-server/node_modules/hapi/lib/plugin.js:216:25)
at CWD/node_modules/electrode-server/lib/electrode-server.js:238:16
at CWD/node_modules/electrode-server/lib/electrode-server.js:237:29
From previous event:
at registerPlugins (CWD/node_modules/electrode-server/lib/electrode-server.js:234:20)
From previous event:
at startElectrodeServer (CWD/node_modules/electrode-server/lib/electrode-server.js:294:6)
at CWD/node_modules/electrode-server/lib/electrode-server.js:365:14
From previous event:
at start (CWD/node_modules/electrode-server/lib/electrode-server.js:354:71)
at processImmediate (internal/timers.js:464:21)
From previous event:
at electrodeServer (CWD/node_modules/electrode-server/lib/electrode-server.js:412:6)
at Object.<anonymous> (CWD/node_modules/electrode-archetype-react-app-dev/lib/dev-admin/dev-server.js:23:3)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
at internal/main/run_main_module.js:17:47
Unhandled rejection Error: Invalid plugin options {
"__name": "webpackDevHapi",
"module": "./dev-hapi.js",
"requireFromPath": "CWD/node_modules/electrode-archetype-react-app-dev/lib/dev-admin"
}
[1] "attributes" is required
at Object.exports.assert (CWD/node_modules/electrode-server/node_modules/hoek/lib/index.js:740:11)
at Object.exports.apply (CWD/node_modules/electrode-server/node_modules/hapi/lib/schema.js:17:10)
at module.exports.internals.Server.internals.Plugin.register (CWD/node_modules/electrode-server/node_modules/hapi/lib/plugin.js:216:25)
at CWD/node_modules/electrode-server/lib/electrode-server.js:238:16
at CWD/node_modules/electrode-server/lib/electrode-server.js:237:29
From previous event:
at registerPlugins (CWD/node_modules/electrode-server/lib/electrode-server.js:234:20)
From previous event:
at startElectrodeServer (CWD/node_modules/electrode-server/lib/electrode-server.js:294:6)
at CWD/node_modules/electrode-server/lib/electrode-server.js:365:14
From previous event:
at start (CWD/node_modules/electrode-server/lib/electrode-server.js:354:71)
at processImmediate (internal/timers.js:464:21)
From previous event:
at electrodeServer (CWD/node_modules/electrode-server/lib/electrode-server.js:412:6)
at Object.<anonymous> (CWD/node_modules/electrode-archetype-react-app-dev/lib/dev-admin/dev-server.js:23:3)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
at internal/main/run_main_module.js:17:47
Electrode webpack dev server exited code 0 - signal null

SyntaxError: Unexpected token '||=' on import of pdfjs-dist

Trying to use pdfjs-dist in node js app
Steps:
created new node js app (module) then:-
npm i pdfjs-dist
package.json is:-
"name": "testpdf",
"version": "1.0.0",
"description": "pdf test",
"main": "testpdf.js",
"type": "module",
"scripts": {
"start": "node testpdf.js"
},
"dependencies": {
"pdfjs-dist": "^2.13.216"
}
}
1 line app (testpdf.js) is
import pdfjs from 'pdfjs-dist'
Get this error with the import:
D:\dev\pdf\node_modules\pdfjs-dist\build\pdf.js:1707
return this._jsActionsPromise ||= this._transport.getPageJSActions(this._pageIndex);
^^^
SyntaxError: Unexpected token '||='
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at ModuleWrap. (internal/modules/esm/translators.js:199:29)
at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
at async Loader.import (internal/modules/esm/loader.js:166:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5) PS D:\dev\pdf>
If I look at node_modules\pdfjs-dist\build\pdf.js there are multiple instances of use of this operator "||="
I'm no javascript expert but from what I know this is not a valid opertaor and node v14.15.4 doesn't seem to think it is either.
Note this package version (pdfjs-dist 2.13.216) has been available for two months and can't find a similar issue on the net so guessing I am doing something wrong...
Any ideas?
As was on and old version of NodeJS I upgraded to the latest version v18.1.0 and this cleared the issue

use the Dbus package in an electron app (Build process)

i have this problem when i require DBus from the dbus package
const { app, BrowserWindow } = require("electron");
const DBus = require('dbus');
app.whenReady().then(() => {
var win = new BrowserWindow({ width: 600, height: 500 });
win.loadFile('index.html');
win.webContents.openDevTools();
});
so when i execute this code i get this error :
> electron script.js
App threw an error during load
Error: Module did not self-register: '/home/bislem/projects/test/electron/node_modules/dbus/build/Release/dbus.node'.
at process.func [as dlopen] (electron/js2c/asar.js:140:31)
at Object.Module._extensions..node (internal/modules/cjs/loader.js:1196:18)
at Object.func [as .node] (electron/js2c/asar.js:140:31)
at Module.load (internal/modules/cjs/loader.js:981:32)
at Module._load (internal/modules/cjs/loader.js:881:14)
at Function.Module._load (electron/js2c/asar.js:769:28)
at Module.require (internal/modules/cjs/loader.js:1023:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/home/bislem/projects/test/electron/node_modules/dbus/lib/dbus.js:3:13)
at Module._compile (internal/modules/cjs/loader.js:1145:30)
A JavaScript error occurred in the main process
Uncaught Exception:
Error: Module did not self-register: '/home/bislem/projects/test/electron/node_modules/dbus/build/Release/dbus.node'.
at process.func [as dlopen] (electron/js2c/asar.js:140:31)
at Object.Module._extensions..node (internal/modules/cjs/loader.js:1196:18)
at Object.func [as .node] (electron/js2c/asar.js:140:31)
at Module.load (internal/modules/cjs/loader.js:981:32)
at Module._load (internal/modules/cjs/loader.js:881:14)
at Function.Module._load (electron/js2c/asar.js:769:28)
at Module.require (internal/modules/cjs/loader.js:1023:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/home/bislem/projects/test/electron/node_modules/dbus/lib/dbus.js:3:13)
at Module._compile (internal/modules/cjs/loader.js:1145:30)
I searched for this error and tried some solutions but none of them worked (npm rebuild,npm update,delete the node_modules folder then npm install,use a diffrent version of node using the nvm command), but when runing each package by his own, (run electron using the start script electron ., and run dbus with node script.js) they works correctly.
and there was this when installing the dbus package :
console when installing dbus, console when installing dbus part 2
my package.json file :
{
"name": "electrontest",
"version": "1.0.0",
"description": "",
"main": "script.js",
"scripts": {
"start": "electron script.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bootstrap": "^4.5.3",
"dbus": "^1.0.7",
"electron": "^10.1.5"
}
}
so my question is what is the correct way to Build/Run such code ? || how to use these two packages side by side ?
You need to run
npm install --save-dev electron-rebuild && npx electron-rebuild
More on using native node modules in electron here

Can't deploy functions with Crashlytics triggers

I write many functions that trigger by Firebase features and I can deploy them but only functions that trigger by Crashlytics I can't deploy.
exports.sendOnVelocityAlert = functions.crashlytics.issue().onVelocityAlert(event => {
const data = event.data;
const issueId = data.issueId;
const issueTitle = data.issueTitle;
const appName = data.appInfo.appName;
const appPlatform = data.appInfo.appPlatform;
const latestAppVersion = data.appInfo.latestAppVersion;
const crashPercentage = data.velocityAlert.crashPercentage;
const slackMessage = `<!here|here> There is an issue ${issueTitle} (${issueId}) ` + `in ${appName}, version ${latestAppVersion} on ${appPlatform} that is causing ` + `${parseFloat(crashPercentage).toFixed(2)}% of all sessions to crash.`;
return notifySlack(slackMessage)/then(() => {
console.log(`Posted velocity alert ${issueId} successfully to Slack`);
});
});
I always see this error "Cannot read property 'issue' of undefinded" when I deploy.
i deploying functions
i functions: ensuring necessary APIs are enabled...
✔ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
Error: Error occurred while parsing your function triggers.
TypeError: Cannot read property 'issue' of undefined
at Object.<anonymous> (/private/var/folders/y8/tbbq9y5j12jc5rlwcmt5smgm0000gn/T/fbfn_10408f4KU3uXJsLQc/index.js:261:53)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:18:11
at Object.<anonymous> (/usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:32:3)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
I use Firebase CLI v3.16.0, node v8.9.3, npm v5.5.1 and version of dependencies in package.json are below.
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"dependencies": {
"#google-cloud/storage": "^1.5.1",
"child-process-promise": "^2.2.1",
"firebase-admin": "^5.6.0",
"firebase-functions": "^0.7.5",
"mkdirp": "^0.5.1",
"mkdirp-promise": "^5.0.1",
"request": "^2.83.0",
"request-promise": "^4.2.2",
"uuid": "^3.1.0"
},
"private": true
}
Any ideas to fix this problem?
The Firebase CLI runs the code you have locally to parse your triggers. Though your package.json has a completely valid dependency, it looks like the version of firebase-functions you have installed is from before firebase.crashlytics was a introduced in October.
Could you try running npm install from your functions directory and see if that fixes the issue?

Node.js cfenv causes TypeError on localhost

In my app.js, the line "var cfenv = require 'cfenv';" causes
path.js:8
throw new TypeError('Path must be a string. Received ' +
^
TypeError: Path must be a string. Received undefined
at assertPath (path.js:8:11)
at Object.posix.join (path.js:479:5)
at getPortsFile (/Users/user1/Dev/src/bluemix/dirt/node_modules/ports/index.js:62:15)
at Object.<anonymous> (/Users/user1/Dev/src/bluemix/dirt/node_modules/ports/index.js:13:29)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
the module 'cfenv' is installed in the local node_modules and the app works on Bluemix.
Here's the package.json, but the versions don't seem to impact it. this only recently started to cause errors, but I cannot trace the origin...
"dependencies": {
"express": "4.12.4",
"cfenv": "1.0.3"
},
"repository": {},
"engines": {
"node": "0.12.x"
}
You have to include your cfenv package, for example using the following code:
var cfenv = require ("cfenv");
From your source I see that you aren't using the correct syntax require('package')
var cfenv = require 'cfenv';
you can see that round brackets are missing.

Resources