Ubuntu node require mongodb fails



  • Hi guys -
    I have just installed jsreport on an Ubuntu server. I followed some instructions found here: https://jsreport.net/learn/ubuntu.
    INstall seems totally fine until running a report.
    My problem is that every one of my reports pulls from MongoDB so I have beforeRender script for them all and every one of them has to require('mongodb').MongoClient;

    When I try to render a report I'm getting the error below. I can't find any information on "unable to require module mongodb".
    I am really just migrating jsreport from my local mac to Ubuntu (I cloned reports into /data from a working install). I am not a proficient Ubuntu user so don't know why this might happen. Can anyone help?

    confirmed: MongoDB ^4.0.1 is in package.json dependencies. Exact same report I am testing works in another environment (mac).

    Error:
    A critical error occurred while trying to execute the render command (2). Error while executing user script. Unable to require module mongodb. Unexpected token {
    /home/ubuntu/jsreportapp/node_modules/mongodb/lib/bson.js:10
    catch { } // eslint-disable-line
    ^

    SyntaxError: Unexpected token {
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/ubuntu/jsreportapp/node_modules/mongodb/lib/utils.js:10:16).

    1 | const MongoClient = require('mongodb').MongoClient;
    | ^
    2 | const ObjectId = require('mongodb').ObjectID;
    3 | const conns = require('./connStrings');
    4 |

    (1).
    caused by error (2):
    -> stack
    Error:
    at onCriticalError (/home/ubuntu/jsreportapp/node_modules/jsreport-cli/lib/commands/render.js:302:19)
    at exports.handler (/home/ubuntu/jsreportapp/node_modules/jsreport-cli/lib/commands/render.js:298:12)
    at <anonymous>
    caused by error (1):
    -> meta = {"weak":true,"logged":true}
    -> stack
    Error: Unable to require module mongodb. Unexpected token {
    /home/ubuntu/jsreportapp/node_modules/mongodb/lib/bson.js:10
    catch { } // eslint-disable-line
    ^

    SyntaxError: Unexpected token {
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/ubuntu/jsreportapp/node_modules/mongodb/lib/utils.js:10:16)
    at safeRequire (/home/ubuntu/jsreportapp/node_modules/jsreport-core/lib/render/safeSandbox.js:201:15)
    at doRequire (/home/ubuntu/jsreportapp/node_modules/jsreport-core/lib/render/safeSandbox.js:208:8)
    at _require (/home/ubuntu/jsreportapp/node_modules/jsreport-core/lib/render/safeSandbox.js:66:14)
    at Object.base.apply (/home/ubuntu/jsreportapp/node_modules/vm2/lib/contextify.js:469:32)
    at evaluate-user-script.js:1:21
    at ContextifyScript.Script.runInContext (vm.js:59:29)
    at VM.run (/home/ubuntu/jsreportapp/node_modules/vm2/lib/main.js:219:62)
    at run (/home/ubuntu/jsreportapp/node_modules/jsreport-core/lib/render/safeSandbox.js:171:19)
    at scriptEvalChild (/home/ubuntu/jsreportapp/node_modules/jsreport-scripts/lib/scriptEvalChild.js:210:5)
    at scriptExecModuleWrapper (/home/ubuntu/jsreportapp/node_modules/jsreport-core/lib/scriptExecModuleWrapper.js:34:26)



  • Please try to check the mongodb and nodejs versions compatibility.

    Are you able to do require('mongodb').MongoClient; outside jsreport in normal nodejs script?



  • Good call. It was the node version. Had to update to 10-something to fix the error. Had to also add the new node /bin to environment variable.
    Documenting in case this helps anyone with the same problem.
    Thank you


Log in to reply
 

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