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 8949019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:08:29+00:00 2026-06-15T13:08:29+00:00

I am having a hard time understanding how to export a file and then

  • 0

I am having a hard time understanding how to export a file and then include it elsewhere on node.js.

Suppose I am working on a game and I want to have the variables which define an object, or more than one, for example a var enemy:

var enemy = {
  health: 100,
  strengh: 87
};

and I save it in a file vars.js.

How can one import these variables from anywhere in my project where I need them?

Thanks in advance.

  • 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-15T13:08:30+00:00Added an answer on June 15, 2026 at 1:08 pm

    You would need to export them.

    So Enemy.js:

    var enemy = {
      health: 100,
      strengh: 87
    };
    
    exports.health = enemy.health;
    exports.strength = enemy.strength;
    

    And in otherjsfile.js:

    var Enemy = require('Enemy.js');
    
    
    //and then you can do 
    
    console.log(Enemy.health); ///etc
    

    Side point:

    If the ‘enemy’ information is changing periodically and you want to get the newest value, you would do:

      Object.defineProperty(exports, "health", {
        get: function() {
          return enemy.health;
        }
      }); //instead of `exports.health = enemy.health;`
    
      Object.defineProperty(exports, "strengh", {
        get: function() {
          return enemy.strengh;
        }
      }); //instead of `exports.strength = enemy.strength;`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I having hard time understanding importance and benefits of Annotations and so have two
I'm having a hard time understanding why #include <iostream> using namespace std; int fib(int
I have been having a hard time understanding how to use a singleton to
I am having a hard time understanding how font-size is working in the following
Edit : People are having a hard time understanding what i want. So here's
I'm having hard time understanding the stateless issue with mvc. Let's say i have
Im having a hard time understanding the limits and possibilities of an IF statement.
I am having a hard time understanding the nuances of git-fetch. I understand that
I am having hard time in understanding Wait() , Pulse() , PulseAll() . Will
I'm having a hard time understanding why the following code doesn't work. I'm sure

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.