Custom module not found on AWS Lambda



  • Hi,

    We are trying to use jsreport on AWS via Lambda functions but we are stuck with a module problem.
    You can see below our project directories :
    0_1632148702796_upload-17f2e57f-205c-4f26-b250-2c1042249416

    The custom modules are saved in the scripts folder.

    When i try to require a custom module in one of my report content.js, I got the following error on AWS (everything is working well locally and the scripts folder is well uploaded to /var/tasks/scripts/... on my lambda).

    {
      "errorType": "Error",
      "errorMessage": "Error while executing user script. Unable to find module ./scripts/database/afpdb\nThe require calls:\nrequire('./scripts/database/afpdb')\nrequire('/var/task/scripts/database/afpdb')\nrequire('/var/runtime/scripts/database/afpdb')\n. \n\n  1 | const _createHelperAfpdb = require('./scripts/database/afpdb');\n    |                            ^\n  2 | const _createHelperCommon = require('./scripts/common');\n  3 | const _createBeforeRenderHandler = require('./scripts/beforerender');\n  4 | const _createHelperIssuer = require('./scripts/database/issuer');\n\n",
      "trace": [
        "Error: Unable to find module ./scripts/database/afpdb",
        "The require calls:",
        "require('./scripts/database/afpdb')",
        "require('/var/task/scripts/database/afpdb')",
        "require('/var/runtime/scripts/database/afpdb')",
        "",
        "    at doRequire (/var/task/node_modules/jsreport-core/lib/render/safeSandbox.js:220:11)",
        "    at _require (/var/task/node_modules/jsreport-core/lib/render/safeSandbox.js:66:14)",
        "    at Object.base.apply (/var/task/node_modules/vm2/lib/contextify.js:469:32)",
        "    at evaluate-user-script.js:2:28",
        "    at Script.runInContext (vm.js:130:18)",
        "    at VM.run (/var/task/node_modules/vm2/lib/main.js:219:62)",
        "    at run (/var/task/node_modules/jsreport-core/lib/render/safeSandbox.js:171:19)",
        "    at scriptEvalChild (/var/task/node_modules/jsreport-scripts/lib/scriptEvalChild.js:210:5)",
        "    at scriptExecModuleWrapper (/var/task/node_modules/jsreport-core/lib/scriptExecModuleWrapper.js:34:26)",
        "    at process.<anonymous> (/var/task/node_modules/script-manager/lib/worker-processes.js:66:36)",
        "    at process.emit (events.js:314:20)",
        "    at emit (internal/child_process.js:877:12)",
        "    at processTicksAndRejections (internal/process/task_queues.js:85:21)"
      ]
    }
    

    Do you have any idea ?

    Thanks a lot,

    Regards,



  • Please try it like this

    const path = require('path')
    const myModule = require(path.join(__appDirectory,  'scripts/database/afpdb'))
    


  • Hi Jan,

    Thanks for the quick reply.

    After more digging, it appears that my modules are found but not formatted as you expect.

    Actually, this is not working :

    'use strict';
    
    const _createAccess1 = require('module1');
    const _createAccess2 = require("module2");
    const _createAccess3 = require('module3');
    const _config = require("config");
    const _mongoose = require('mongoose');
    
    module.exports = function () {
    ...
    }
    

    but this is working :

    'use strict';
    
    module.exports = function () {
        const _createAccess1 = require('module1');
        const _createAccess2 = require("module2");
        const _createAccess3 = require('module3');
        const _config = require("config");
        const _mongoose = require('mongoose');
    
    ....
    }
    

Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.