简体   繁体   中英

Can't import module with require() in nodejs

I'm developing a nodejs webapp and I have a problem. I have a twitter.js module that expose some functions to use in other files. My main application, app.js, import it correctly and can use the functions. The problem is, if I import it in another .js file (via the var twitter = require(./twitter) method), it doesn't load, I can see that via console.log(twitter) and also because it doesn't recognize the functions. This happens if app.js requires it. I copied the twitter.js file and name it differently, and if I import that it works. How can I solve this problem? Thanks

EDIT: Adding some code

This is the main app, app.js

var express = require("express");
var bodyParser = require("body-parser");
var path = require("path");

var twitter = require("./twitter");
var mongo = require("./mongo");
var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}))

app.use(express.static(path.join(__dirname,'public')));

app.set('view engine','ejs');
app.set('views',path.join(__dirname,'views'));

app.get("/",function(req,res){
    res.render("index.ejs")
});
app.get("/twitter",function(req,res){
    twitter.getTrends(res);
});
app.post("/sendMessage",function(req,res){
    twitter.postTwit(req.body.valore);
});
app.get("/pullData",function(req,res){
    mongo.pullData(res);
});
app.listen(8080, function(){
    console.log("server started on 8080");
});

Next is the twitter.js file:

var Twitter = require('twitter'); //import package
var mongo = require("./mongo");

var T = new Twitter({ ---developer keys here --})

exports.getTrends = function(res){

  var params = { id: 23424853, count: 10}
  T.get('trends/place',params,gotData);

  function gotData(err,data,response){
      res.render('tweets',{ data: data[0], length: 10});}
}
exports.postTwit = function(value){
  T.post('statuses/update', {status:value}, function(error, tweet, response)       {
    mongo.pushdata(tweet.id_str);
      });
}
exports.loadTwit = function(res,value){
var params = {id: value}
T.get('statuses/show/', params, function (err, data, response) {
  console.log(data);
  res.render('db',{ data: data});
});
}

and finally the mongo.js file, for the mongo database

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
var twitter = require('./twitter');

exports.pushdata = function(value){
  MongoClient.connect(url, function(err, db) {
    if (err) throw err;
    var myobj = {id: value};
    db.collection("tweets").insertOne(myobj, function(err, res) {
      if (err) throw err;
      db.close();
    });
  });
}
exports.pullData = function(res){
  MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  db.collection("tweets").find({}).toArray(function(err, result) {
    result.forEach(function(entry) {
    twitter.loadTwit(res,entry);
});
    db.close();

  });
});
}

all these files are in the same folder.

You have created circular dependency between mongo.js & twitter.js .

mongo.js tries loading twitter.js which in turn requires mongo.js . Thats the problem. Remove this circular dependency.

Maybe you can pass the twitter object as a parameter when you call the functions of mongo.js . Or something else.

For eg

mongo.js

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

// pass the twitter object as a parameter
exports.pullData = function(twitter, res){
  MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  db.collection("tweets").find({}).toArray(function(err, result) {
    result.forEach(function(entry) {
    twitter.loadTwit(res,entry);
});
    db.close();

  });
});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM