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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:11:55+00:00 2026-06-14T07:11:55+00:00

I have an object in a file and want to be able to require

  • 0

I have an object in a file and want to be able to require that file and then create new instances of the object at whim, but I’ve hit a snag. This seems so incredibly basic, what am I missing.

hat.js

function Hat(owner) {
    this.owner = owner;
}
Hat.prototype.tip = function() {
    console.log("and he (" + owner + ") tipped his hat, just like this");
}
exports.Hat = Hat;

node terminal

Attempt 1

> require('./hat.js');
> var mighty_duck = new Hat('Emilio');
  ReferenceError: Hat is not defined

Attempt 2

> var Hat = require('./hat.js');
> var mighty_duck = new Hat('Emilio');
  { owner: 'Emilio' }
> mighty_duck.tip();
  TypeError: Object #<Hat> has no method 'tip'

Edit

I, most unfortunately, left out what turned out to be the biggest problem bit. The fact that I was trying to use

util.inherits(Hat, EventEmitter);

So my hat.js would actually be

function Hat(owner) {
    this.owner = owner;
}
Hat.prototype.tip = function() {
    console.log("and he (" + owner + ") tipped his hat, just like this");
}
util.inherits(Hat, EventEmitter);
exports.Hat = Hat;

This is an issue, because, apparently it is important to have your inherits call before you start extending the prototype. The fix is simple, move the inherits call up a few lines

function Hat(owner) {
    this.owner = owner;
}
util.inherits(Hat, EventEmitter);
Hat.prototype.tip = function() {
    console.log("and he (" + owner + ") tipped his hat, just like this");
}
exports.Hat = Hat;
  • 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-14T07:11:57+00:00Added an answer on June 14, 2026 at 7:11 am

    You are doing your require wrong:

    var Hat = require('./hat.js').Hat;
    

    Is what you want. When you do exports.Hat = Hat; you are exporting an object (exports) with a propery of Hat. So when you require './hat.js' you get the object, and need to access the Hat property.

    From node repl:

    > require('./hat.js');
    { Hat: [Function: Hat] }
    > require('./hat.js').Hat;
    [Function: Hat]
    > var Hat = require('./hat.js').Hat;
    undefined
    > var mighty_duck = new Hat('Emilio');
    undefined
    > mighty_duck.tip();
    and he (Emilio) tipped his hat, just like this
    

    Of course that was causing an error since you said owner and not this.owner in tip(), but after I changed that it worked fine 🙂

    If you want to just do

    var Hat = require('./hat.js'),
        hat = new Hat('Emilio');
    

    Then change your export to:

    module.exports = exports = Hat;
    

    And it works:

    > require('./hat.js');
    [Function: Hat]
    > var Hat = require('./hat.js');
    undefined
    > var hat = new Hat('Emilio');
    undefined
    > hat.tip();
    and he (Emilio) tipped his hat, just like this
    

    EDIT Cleaned up and inherits from EventEmitter:

    var util = require('util'),
        events = require('events');
    
    var Hat = exports.Hat = function(owner) {
        events.EventEmitter.call(this);
        this.owner = owner;
    }
    
    util.inherits(Hat, events.EventEmitter);
    
    Hat.prototype.tip = function() {
        console.log("and he (" + this.owner + ") tipped his hat, just like this");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have one directory for all object files and create Common.pri file
I want to create a 'File' object that returns 'Line' objects when the ReadLine()
I have an object that requires the URL of a file that resides in
Imagine you have an object foo that you saved as saved.file.rda as follows: foo
I have the following code that saves an object to a file... -(int) saveObject:
I have a C++ object file that contains instantiations of some C++ template functions.
I have an index.php file that has 3 includes <?php require_once('mod.php'); $mod = new
Question is simple, i have a object file and i want to read the
I have a .properties file I want to load in a Java Properties object.
I have two Java.io.File objects file1 and file2. I want to copy the contents

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.