jsreport beforeRender problem with Error: Unable to find module foo



  • Hi All,
    At the beginning of beforeRender function, when i require a script folder, it fails with the error
    "Error: Unable to find module foo" . This folder place at /opt/jsreport/node_modules folder and contains some scripts, package.json, index.js as main in package.json, and node_modules folder with package dependencies.

    function beforeRender(done) {
    	var console = require('console');
    
                // Error happens at following line of code
    	var ods = require('foo');
    
    	var ds = new ods.CouchbaseDS();
    	var options = request.options.parameters;
    	var recipe = request.options.recipe;
    
    	console.log(request.options);    
    
    	ds.open(function(err) {
    		if (err) {
    			console.log('Error on opening logdb', err);     
    			return done();
    		}  
    
    		var ts = options.hasOwnProperty('time_scale') && options.time_scale !== '' ?
    			options.time_scale : 1;             
    
    		ds.queryLog({
    			queue: options.queue,
    			interval_start: options.start_date.valueOf(),
    			interval_end: options.stop_date.valueOf(),
    			time_scale: ts
    		}, function(err, result) {
    			if(err)
    				console.log(err);
    			else {
    				result.queue.recipe = recipe;
    				request.data = result;
    				done();
    			}
    		});  
    	});
    }
    

    Error context is as follows:

    	Error occured - Error during rendering report: Unable to find module foo
    	Searched paths: 
    	foo
    	foo
    	/opt/jsreport/foo
    	/opt/jsreport/foo
    	/opt/jsreport/foo
    
    	Stak - /opt/jsreport/node_modules/jsreport-scripts/lib/scriptEvalChild.js:25
    		  throw new Error('Unable to find module ' + moduleName + os.EOL + 'Searched paths: ' + os.EOL + searchedPaths)
    		  ^
    
    	Error: Unable to find module foo
    	Searched paths: 
    	foo
    	foo
    	/opt/jsreport/foo
    	/opt/jsreport/foo
    	/opt/jsreport/foo
    
    		at doRequire (/opt/jsreport/node_modules/jsreport-scripts/lib/scriptEvalChild.js:25:13)
    		at _require (/opt/jsreport/node_modules/jsreport-scripts/lib/scriptEvalChild.js:41:14)
    		at beforeRender (evalmachine.<anonymous>:3:13)
    		at evalmachine.<anonymous>:71:111
    		at ContextifyScript.Script.runInContext (vm.js:59:29)
    		at ContextifyScript.Script.runInNewContext (vm.js:65:15)
    		at Object.runInNewContext (vm.js:135:38)
    		at module.exports (/opt/jsreport/node_modules/jsreport-scripts/lib/scriptEvalChild.js:123:6)
    		at process.<anonymous> (/opt/jsreport/node_modules/script-manager/lib/worker-processes.js:45:38)
    		at emitTwo (events.js:126:13)
    

    Any Ideas?
    can you tell me some tips so that i solve my problem.
    Thanks!



  • Hi, I assume foo is a custom package you added by yourself ?
    Did you look at : https://jsreport.net/learn/configuration#templating-engines-configuration ? it is possible that the module is not allowed to load hence, cannot be found ? (I would look into that first, but I still may be wrong)



  • Hi @MI53RE19 , thank you for your attention. Yes, I took care about allowedModules .

    here is this part of prod.config.json:
    "scripts": {
    "allowedModules": ["handlebars", "request-json", "feedparser", "request", "underscore",
    "constants", "sendgrid", "console", "foo"],
    "timeout": 120000
    },

    but the Error still.
    regards



  • As I included error in first message, "foo" folder placed at /opt/jsreport/node_modules but it seems that jsreport searches it in /opt/jsreport folder and I don't know why!
    Although I know (if I correctly understand) require searches in current directory and then ./node_modules . if is it correct, why jsreport can not find "foo"?

    Is there any configuration item for this search. For test I got the __rootDirectory and __appDirectory global variable and logged them and both of them are pointed to /opt/jsreport. Is it correct?



  • Did you perhaps added manualy that module to your node_module folder without passing by npm intall foo ? If so take a look at this link :
    https://stackoverflow.com/questions/16652620/node-js-require-cannot-find-custom-module
    they do explain that you should use relative path to acces your module, certainly becouse it is not available in the node.js module's index table



  • What is in your foo module?
    We throw the "unable to find module" every time when the require('foo') crashes.
    Maybe it is found, but there is some error inside.
    It is likely the issue because the require('console') didn't fail for you.



  • Hi @jan_blaha , "foo" is a directory that contain some custom scripts and package.json .
    here is my directory structure:

    /opt
    /jsreport
    /node_modules
    /foo
    index.js
    package.json
    /node_modules
    /lib
    couchbase-ds.js
    surveyReport.js

    and package.json is as follows:

    	{
    	  "name": "foo",
    	  "main": "index.js",
    	  "dependencies": {
    		"couchbase": ">= 2.0.7",
    		"esl": ">= 3.0.0",
    		"hiredis": ">= 0.3.0",
    		"modesl": ">= 1.1.3",
    		"mysql": ">= 2.6.2",
    		"redis": ">= 0.12.1",
    		"ssh2": ">= 0.4.13",
    		"utf8": "^2.1.1"
    	  }
    	}
    

    and index.js is here:

               var ods = module.exports = {}
               ods.CouchbaseDS  = require("./lib/couchbase-ds.js").CouchbaseDS;
               ods.SurveyReport = require("./lib/surveyReport.js").SurveyReport;
    

    It's important that main script in surveyReport.js executes without any error and returns desired data structure,
    when i run it as test script on the same machine. also it seems that there isn't any faulty code in it.



  • Hi @jan_blaha , in conjunction with your suggestion about "foo", i tried to test some simple code instead of "couchbase-ds.js".
    Unfortunately the result is not changed and i see the same error again: "Unable to find module foo".

    here is the sample code in :
    couchbase-ds.js

    	var CouchbaseDS = exports.CouchbaseDS = function(context) {
    		this.bucket = null;
    	}
    
    	CouchbaseDS.prototype.open = function(bucket_name, cb) {
    		return cb && cb(null);
    	}
    
    
    	CouchbaseDS.prototype.queryLog = function(key, cb) {
    		var now = new Date();
    		
    		var al = {
    			days: [], 
    			queue: {
    				name: key.queue,
    				reportDate: formatDate(now.valueOf()),
    				startDate: key.interval_start != undefined ? formatDate(key.interval_start) : '',
    				endDate: key.interval_end != null ? formatDate(key.interval_end) : ''
    			}
    		};
    
    		return cb && cb(null, al);
    	}
    
    	CouchbaseDS.prototype.queryBriefActivityLog = function(key, cb) {
    		var now = new Date();
    
    		var al = {
    			days: [], 
    			entity: {
    				name: key.queue ? key.queue : key.user[0],
    				reportDate: formatDate(now.valueOf()),
    				startDate: key.interval_start != undefined ? formatDate(key.interval_start) : '',
    				endDate: key.interval_end != null ? formatDate(key.interval_end) : ''
    			}
    		};
    	  
    		return cb && cb(null, al);
    	}
    
    	function formatDate(x) {
    		var d = new Date(x);
    		return [
    			d.getFullYear(), 
    			d.getMonth()+1, 
    			d.getDate()
    		].join('/') + ' ' + [
    			d.getHours(),
    			d.getMinutes(),
    			d.getSeconds()
    		].join(':');  
    	}
    

    Therefore i think that the error is not depends on scripts. Are you agree? What can I check?
    Thanks in advanced.



  • Hi @jan_blaha again, a point that i remembered now is my code works fine on another server and the only difference is
    that node version there is v4.4.7 but here it is v8.14.1 , although I have installed foo with npm install .. that make the dependencies
    up to date . Jsreport version is "jsreport 1.4.0 production" on both servers.
    Can this difference be the source of error? although as i mentioned in last posts the script executes on this machine without error!!!!



  • Hi @jan_blaha , in conjunction with your suggestion about "foo", i tried to test some simple code instead of "couchbase-ds.js".
    Unfortunately the result is not changed and i see the same error again: "Unable to find module foo".

    I am getting a bit lost in it. So if you put to foo just a dummy index.js. It crashes also?
    I don't know if we are solving the problem that the foo is not found or it has an actual error.

    that node version there is v4.4.7 but here it is v8.14.1 ,

    Well your script can use some features that are not supported in 4.4.7 and crash.



  • Btw you should later check this thread. There is a bug in couchbase node client which can cause the queries hanging in our sandbox.
    https://forum.jsreport.net/topic/819/unable-to-fetch-data-from-remote-server-in-beforerender/6



  • Hi @jan_blaha and thank you for help. I've removed index.js from foo directory, so there is no relation to my custom code
    and tried to test again, but the error is still and is as expected "Unable to find module foo".
    It seems as i commented in "berforeRender", that error occurred in second line of code:

               var ds = require('foo');
    

    and none of my script does not executed. Thus can I explain why after removing index.js
    there is no difference in error in both cases. I think actually jsreport can not find foo. In addition
    the error log that i included in first post shows that

                 search path:
                   foo
                   foo
                   /opt/jsreport/foo
                   ....
    

    jsreport searches foo in /opt/jsreport directory that is false, it must be /opt/jsreport/node_modules.
    Is it correct? Have I understand correctly?

    regards



  • Hi @jan_blaha , i tried everything that I've known and now I've confused. Are you recommend me to reinstalling jsreport from scratch?
    I think probably there was a mistake in installing or configuring jsreport!



  • Hi @jan_blaha and thank you for help. I've removed index.js from foo directory, so there is no relation to my custom code

    You removed the index.js but kept the package.json? In that case the require('foo') still crashes.
    Please make sure your package actually works when you call require('foo') from the server.js file in the application root.

    I think actually jsreport can not find foo. In addition
    the error log that I included in first post shows that

    These paths are actually correct. I have the same. You get exactly this error if you put something like throw new Error('xxx') to the index.js file.

    So far I still think your foo package is for some reason not working properly.



  • Hi @jan_blaha , Thanks a lot for your recommendations, finally I found the crashes module, that used a postgres without installing it
    on this server. You save my life!!! I've completely confused
    Thanks again


Log in to reply
 

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