Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8497879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:15:03+00:00 2026-06-11T00:15:03+00:00

I am new to Node.js and trying to figure out how to request an

  • 0

I am new to Node.js and trying to figure out how to request an object from a separate file (rather than just requesting a function) but everything I try–exports,module-exports,etc–is failing.

So, for example, if I have foo.js:

    var methods = {
                   Foobar:{
                            getFoo: function(){return "foo!!";},
                            getBar: function(){return "bar!!";}
                   }
                  };
module.exports = methods;

And now I want to call a function within an object of foo.js from index.js:

var m = require('./foo');  
function fooMain(){
  return m.Foobar.getFoo();
};

How do I do this? I have tried all sorts of combinations of exports and module-exports but they seem to only work if I call a discrete function that is not part of an object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:15:05+00:00Added an answer on June 11, 2026 at 12:15 am

    You said that you tried exports, but your code doesn’t show it. Anything that you want to be visible from outside your module must be assigned to (or otherwise be referable from) module.exports. In your case, where you have an object already, you can just assign it to module.exports:

    var methods = {
        ...
    };
    
    // You must export the methods explicitly
    module.exports = methods;
    

    module.exports isn’t magic, it’s a normal object, and you can treat it as such. Meaning that you could have assigned your methods directly to it, as in:

    module.exports.Foobar = {};
    module.exports.Foobar.getFoo = function() { ... };
    ...
    

    Or, as you probably know, you could event replace it with a function:

    module.exports = function() { return "It's ALWAYS over 9000!!!!"; };
    

    Only after exporting will you be able to use anything in another module.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out how to implement a function in a tree node
I'm new to Node.js and I'm trying to figure out what is wrong with
I am new to Drupal and I am trying to figure out how to
I'm trying to figure out how to have Spring Security return a UserDetails object
I'm trying to figure out how to migrate data from one cassandra cluster, to
I'm trying to figure out how to delete an element (and its children) from
I am trying to figure out how to use a node graph for processing
I'm trying to figure out different ways to achieve very large file uploads/downloads. I'm
New to Node.js and Express, I am trying to understand the two seems overlapping
I'm trying to add a new nova-compute node (KUubuntu 12.04) to my single node

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.