Reputation: 173
I'm creating a crawler to parse a game I have this function on the main file (crawler.js)
function kicked() {
try {
info.logged = false;
info.next_login = 0;
info.login_tries = 0;
report('Crawler disconnected from game, reconnecting.');
}
catch (error) {
report('Disconected() error => ' + error);
}
}
And i have
module.exports = { kicked: kicked };
and in another file (renew session) i have
var crawler = require('../../crawler');
but when i call the crawler.kicked()
i get undefined and if i use console.log(crawler);
it shows an empty object no errors nothing, just an empty object and i can't find why the file isn't exporting the function any help ?
Upvotes: 10
Views: 43452
Reputation: 187
Its happening because of circular dependency resolution.
A -> B -> A
To fix this, don't use require
at the top of the file, use it inside the required function.
function test(){
const x = require('../file');
...
}
Upvotes: 0
Reputation: 7188
The problem is that you have cyclic dependencies, which is what happens when A requires B, which requires A, etc. These are not always a problem. Node can handle cyclic dependencies. However, without a very good understanding of how Node modules are resolved, you are likely to run into problems (usually the empty-object problem).
Let's take a closer look at what's going on here:
crawler.js
), which begins to execute.crawler.js
requires session.js
.session.js
starts to execute its top-level code. Remember that crawler.js
has not finished executing its own top-level code.session.js
requires crawler.js
. But crawler.js
has nothing to export (yet). It hasn't yet reached the bottom of the file (it's still up at the top executing its require calls).So now we can see what's going on. The empty object is the default exports object. When you do module.exports = {...}
, you reassign the module's exports
property to a new object. But module.exports
has not yet been reassigned, for the reasons mentioned above.
The solution will be to rethink your code. I suggest trying to eliminate circular dependencies altogether; once you have a better grasp of how they work, you may want to use them from time to time. Your main file should probably not have any exports. Try putting your kicked
function in another file, and require that where needed.
Upvotes: 31
Reputation: 560
you can also directly assign the function as a property of module.exports, as the following:
module.exports.kicked = () => ...
Though this will work, you need to specify module.exports.kicked() every time you want to call the function, which, for internal file use, can get annoying. But not a bad quick-fix solution
Upvotes: 1
Reputation: 11
my code works when I remove the '/'.. Here is what I wrote and it works fine. require('.emily'); So you can write var crawler = require('.crawler'); and let me know how us know if you have solved this.
Upvotes: 0
Reputation: 4161
Add .js
to the end of the word crawler
in your require
. You need to specify the file, as opposed to a name.
There is a good article here. A little outdated, but I think it's still pretty accurate.
Here's a snippet:
First, Node.js looks to see if the given module is a core module - Node.js comes with many modules compiled directly into the executable binary (ex. http, fs, sys, events, path, etc.). These core modules will always take precedence in the loading algorithm.
If the given module is not a core module, Node.js will then begin to search for a directory named, "node_modules". It will start in the current directory (relative to the currently-executing Javascript file in Node) and then work its way up the folder hierarchy, checking each level for a node_modules folder.
...
If it still can't find the file in this directory-spidering manner, Node.js will then proceed to look at the directory paths outlined in the "require.paths" array. The paths values in this array default to the paths defined, in part, by the environmental variable, NODE_PATH; but, they can be updated programmatically within a Node.js application.
Upvotes: 0