Porting node module to v8

35 views
Skip to first unread message

Gautham B A

unread,
Jan 12, 2017, 9:53:38 AM1/12/17
to nodejs
Hi all,

I'm trying to port a nodejs module called "sweet.js" into v8. I'm doing this by resolving the dependencies. Specifically, I'm replacing the calls to "require()" method of a module by replacing it with the actual code of the dependency.
e.g. Contents of "a.js"
var b=require('./b');

Contents of "b.js"
function(){
 console.log('inside b');
}

Now I replace the contents of "a.js" as -
var b=(function(){
             console.log('inside b');
            });

However, I got stuck when I tried to resolve circular dependencies. "require()" somehow takes care of resolving circular dependencies "silently" as it says in the docs.
Could anyone please shed some light regarding this?

Christopher Mina

unread,
Jan 16, 2017, 4:09:55 AM1/16/17
to nodejs
I've had success resolving circular dependencies in Node by exporting the declaration of the model in one of the dependents before invoking the circular reference.  

For instance

a.js
var b = require('./a');

module.exports = function() {
   
var _b = new b();
    _b
.initialize();
};



b.js
var b = function() { };
module.exports = b;

var a = require('./a');

b
.prototype.initialize = function() {
   
var _a = new a();
}

Of course the above code example would lead to some serious stack overflow problems, but the point is, NodeJS shouldn't fail on it just because of the circular dependency.  

With your specific case, you might be able to apply some of the same logic to the port that you're attempting to accomplish. 

Good luck. 
Reply all
Reply to author
Forward
0 new messages