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

  • Home
  • SEARCH
  • 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 8500763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:01:37+00:00 2026-06-11T01:01:37+00:00

I’m trying to use nodejs on a project and needed to include a file

  • 0

I’m trying to use nodejs on a project and needed to include a file that contains an object. the mere inclusion did not work. I’ve been researching and found that I had to convert the file to a module. from there I added the line “var generator = require(‘./generator.js’);” to “app.js” and “exports.Generator = new Generator ();” to the file includes, but resulted in this error:

D:\Users\Miguel Borges\Dropbox\Trabalhos\Lero Lero\nodejs\app.js:14
        var phrase = generator.generatePhrases(1);
                               ^ 
TypeError: Object #<Object> has no method 'generatePhrases'
    at Server.<anonymous> (D:\Users\Miguel Borges\Dropbox\Trabalhos\Lero Lero\nodejs\app.js:14:25)
    at Server.EventEmitter.emit (events.js:91:17)
    at HTTPParser.parser.onIncoming (http.js:1783:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:111:23)
    at Socket.socket.ondata (http.js:1680:22)
    at TCP.onread (net.js:410:27)

EDIT

now, with answer of @Brandon Tilley thew error is:

D:\Users\Miguel Borges\Dropbox\Trabalhos\Lero Lero\nodejs\app.js:20
console.log(g.Generator.generatePhrases(1));
                        ^
TypeError: Cannot call method 'generatePhrases' of undefined
    at Object.<anonymous> (D:\Users\Miguel Borges\Dropbox\Trabalhos\Lero Lero\nodejs\app.js:20:25)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

app.js

var uri = '127.0.0.1';
var port = 8001;

var http = require('http');
var url = require('url');
var g = require('./generator.js');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});

    var path = url.parse(request.url).pathname;
//  var generator = new g.Generator();

    var phrase = g.Generator.generatePhrases(1);

    response.end(path + '\n' + phrase);
}).listen(port, uri);

console.log('Server running at http://' + uri + ':' + port + '/');
console.log(g.Generator.generatePhrases(1));

generator.js

function Generator () {

}

Generator.prototype.generatePhrases = function(nrPhrases) {
    return 'hi ' + nrPhrases;
};

// exports.Generator = new Generator();
  • 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-11T01:01:39+00:00Added an answer on June 11, 2026 at 1:01 am

    You want module.exports = new Generator(). The way it is written now, you’d need:

    var generator = require('./generator');
    generator.Generator.generatePhrases(1);
    

    Because

    exports.Generator = new Generator();
    

    says, “When I require this module, I want an object that has a property called Generator; that property is an instance of Generator and has a generatePhrases method.”

    Take a look at my screencast on modules if you need some more explanation about how module.exports works.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has

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.