My Lambda function throws error after deployed on AWS lambda, Jsreport-core version "^2.10.1"
-
I have installed the jsreport-core packages "^2.10.1" in my Node App and deployed on my AWS Lambda services. During Deployment no error throws , after the deployment below error throws and my application got crash on lambda function. Im not sure where i can find the solution or what is the root cause of this.
ERROR Uncaught Exception
{
"errorType": "TypeError",
"errorMessage": "Cannot read properties of undefined (reading 'filename')",
"stack": [
"TypeError: Cannot read properties of undefined (reading 'filename')",
" at resolve (/var/task/dist/index.js:394335:50)",
" at module2.exports (/var/task/dist/index.js:394352:25)",
" at node_modules/app-root-path/index.js (/var/task/dist/index.js:394379:23)",
" at __require (/var/task/dist/index.js:13:50)",
" at node_modules/jsreport-core/lib/reporter.js (/var/task/dist/index.js:394634:19)",
" at __require (/var/task/dist/index.js:13:50)",
" at node_modules/jsreport-core/index.js (/var/task/dist/index.js:398812:20)",
" at __require (/var/task/dist/index.js:13:50)",
" at dist/src/lib/download_service.js (/var/task/dist/index.js:573617:20)",
" at __require (/var/task/dist/index.js:13:50)",
" at dist/src/chargingSessions/invoices.js (/var/task/dist/index.js:573710:30)",
" at __require (/var/task/dist/index.js:13:50)",
" at dist/src/chargingSessions/router.js (/var/task/dist/index.js:573848:22)",
" at __require (/var/task/dist/index.js:13:50)",
" at dist/src/setupRoute.js (/var/task/dist/index.js:621827:21)",
" at __require (/var/task/dist/index.js:13:50)",
" at Object.<anonymous> (/var/task/dist/index.js:622691:20)",
" at Module._compile (node:internal/modules/cjs/loader:1198:14)",
" at Object.Module._extensions..js (node:internal/modules/cjs/loader:1252:10)",
" at Module.load (node:internal/modules/cjs/loader:1076:32)",
" at Function.Module._load (node:internal/modules/cjs/loader:911:12)",
" at Module.require (node:internal/modules/cjs/loader:1100:19)"
]
}Help me to find the solution on this , Thanks in advance folks
-
The error you see will likely get fixed by extra passing the
rootDirectory
.require('jsreport-core')({ rootDirectory: __dirname })
However, using jsreport in lambda won't be that easy anyway. You need to use an extra version of compiled Chrome for example. I would recommend checking our tutorial describing how to run jsreport in aws lambda.
https://jsreport.net/learn/aws-lambda-serverless
-
Hi Admin, Thanks for your response.
As you instructed i have added the rootDirectory: __dirname in my jsreport-core and details given below. Still same issue exist , After deployment on lambda My Node App crash and Not running . throws 500 Error for all API calling. I![alt text](image url) have attached my configurations and Kindly check that and let me know what i missed.import * as _ from "lodash";
const jsreport = require("jsreport-core")({
rootDirectory: __dirname,
});
jsreport.use(require("jsreport-handlebars")());
jsreport.use(require("jsreport-chrome-pdf")());
jsreport.use(require("jsreport-html-to-xlsx")());
jsreport.use(
require("jsreport-assets")({
searchOnDiskIfNotFoundInStore: true,
allowedFiles: "/.",
})
);
jsreport.init();Kindly help me on this. I have added the Jsreport dependency in my layers package.json file also.
{
"name": "testlambda",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"pg": "^8.10.0",
"pg-native": "^3.0.1",
"axios": "^1.3.4",
"jsreport-assets": "^1.7.0",
"jsreport-chrome-pdf": "^1.10.0",
"jsreport-core": "^2.10.1",
"jsreport-handlebars": "^2.1.0",
"jsreport-html-to-xlsx": "2.6.0",
"jsreport-phantom-pdf": "^2.6.1",
"should": "^13.2.3",
"emitter": "^0.0.5"
},
"devDependencies": {
"aws-lambda": "^1.0.7"
}
}2023-07-25T08:22:51.456Z undefined ERROR Uncaught Exception
{
"errorType": "TypeError",
"errorMessage": "Cannot read properties of undefined (reading 'filename')",
"stack": [
"TypeError: Cannot read properties of undefined (reading 'filename')",
" at resolve (/var/task/dist/index.js:394337:50)",
" at module2.exports (/var/task/dist/index.js:394354:25)",
" at node_modules/app-root-path/index.js (/var/task/dist/index.js:394381:23)",
" at __require (/var/task/dist/index.js:13:50)",
" at node_modules/jsreport-core/lib/reporter.js (/var/task/dist/index.js:394636:19)",
" at __require (/var/task/dist/index.js:13:50)",
" at node_modules/jsreport-core/index.js (/var/task/dist/index.js:398814:20)",
" at __require (/var/task/dist/index.js:13:50)",
" at dist/src/lib/download_service.js (/var/task/dist/index.js:573619:20)",
" at __require (/var/task/dist/index.js:13:50)",
" at dist/src/chargingSessions/invoices.js (/var/task/dist/index.js:573714:30)",
" at __require (/var/task/dist/index.js:13:50)",
" at dist/src/chargingSessions/router.js (/var/task/dist/index.js:573851:22)",
" at __require (/var/task/dist/index.js:13:50)",
" at dist/src/setupRoute.js (/var/task/dist/index.js:621973:21)",
" at __require (/var/task/dist/index.js:13:50)",
" at Object.<anonymous> (/var/task/dist/index.js:622837:20)",
" at Module._compile (node:internal/modules/cjs/loader:1198:14)",
" at Object.Module._extensions..js (node:internal/modules/cjs/loader:1252:10)",
" at Module.load (node:internal/modules/cjs/loader:1076:32)",
" at Function.Module._load (node:internal/modules/cjs/loader:911:12)",
" at Module.require (node:internal/modules/cjs/loader:1100:19)"
]
}
-
I don't know, many things can go wrong with old jsreport, new nodejs, mixing import, and require in the same file...
Any reason why not to follow our tutorial?
https://jsreport.net/learn/aws-lambda-serverless
-
In the given Tutorials Those are all manually create function and Layer building. But In our app is Doing everything all by automatic . We have serverless.yaml file to configure those things.
My Doubt is Why we need to clone that Repo first , How do that code will impact with my my Node Application.
Help me to get clear picture on this please. Let me know if you need any additional information.
-
Hi Admin, Greetings of the day.
I have found my lambda function and got the code and i debugged the place and finally i found the place where that actual error is occur.
Code given below for more clarity:
var require_resolve2 = __commonJS({
"node_modules/app-root-path/lib/resolve.js"(exports2, module2) {
"use strict";
var path2 = require("path");
var globalPaths = require("module").globalPaths;
var npmGlobalPrefix;
if ("win32" === process.platform) {
npmGlobalPrefix = path2.dirname(process.execPath);
} else {
npmGlobalPrefix = path2.dirname(path2.dirname(process.execPath));
}
var npmGlobalModuleDir = path2.resolve(npmGlobalPrefix, "lib", "node_modules");
var sep = path2.sep;
module2.exports = function resolve(dirname) {
if (process.env.APP_ROOT_PATH) {
return path2.resolve(process.env.APP_ROOT_PATH);
}
if ("undefined" !== typeof window && window.process && "renderer" === window.process.type) {
var electron = "electron";
var remote = require(electron).remote;
return remote.require("app-root-path").path;
}
var resolved = path2.resolve(dirname);
var alternateMethod = false;
var appRootPath = null;
globalPaths.forEach(function(globalPath) {
if (!alternateMethod && 0 === resolved.indexOf(globalPath)) {
alternateMethod = true;
}
});
var nodeModulesDir = sep + "node_modules";
if (!alternateMethod && -1 !== resolved.indexOf(nodeModulesDir)) {
var parts = resolved.split(nodeModulesDir);
if (parts.length) {
appRootPath = parts[0];
parts = null;
}
}
if (alternateMethod || null == appRootPath) {
appRootPath = path2.dirname(require.main.filename);
}
if (alternateMethod && -1 !== appRootPath.indexOf(npmGlobalModuleDir) && appRootPath.length - 4 === appRootPath.indexOf(sep + "bin")) {
appRootPath = appRootPath.slice(0, -4);
}
return appRootPath;
};
}
});In above code i have highlighted the part where that error occour.
Why that path2.dirname(require.main.filename) la filename property is undefined.Help to get the root cause for this. Im not sure is this directory permission on AWS lambda. If anything like directory changes kindly let me know i will update my User permissions acccordingly.
Waiting for your message .
Folks If you have any insights please feel free to share. Thanks in Advance