inhaler inhaler - 11 months ago 68
Javascript Question

calling a function in another file in javascript using node.js

I have a problem, i am trying to create an object that i can use over and over in a separate javascript file. I have a file called foo.js and another file called boo.js. I want to create the object in boo.js In my server.js file i required foo.js, and it works fine i can access foo.js. I want to be able to access boo.js from foo.js. I keep getting errors when i require boo.js in foo.js and i cant access it. Is There a way to do this?

here is my code

var obj = function () {
return {
data: 'data'

module.exports = {
obj: obj



var request = require('request');
var x = require('./modules/boo')
var random= function() {
return x.obj();

module.exports = {
random: random

Answer Source

If they are in the same directory you will want to require like so var x = require('./boo'). The ./ is relative to the current directory.