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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:56:41+00:00 2026-06-08T02:56:41+00:00

How can I include a file, which contains classes definitions in my server.js file?

  • 0

How can I include a file, which contains classes definitions in my server.js file?

I don’t want to use module.exports because I want to use this file in my client javascript code too.

Thank you!

  • 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-08T02:56:42+00:00Added an answer on June 8, 2026 at 2:56 am

    If you want the module’s contents to be available outside the scope of the file you have to use module.exports. Making the file also work in a browser just requires you to do some extra if/elses

    For instance:

    var self = {};
    
    // Browser?
    if(instanceof window !== 'undefined')
    {
        window['my_module_name'] = self;
    }
    // Otherwise assume Node.js
    else
    {
        module.exports = self;
    }
    
    // Put the contents of this module in self
    // For instance:
    self.some_module_function = function() {
        // Do stuff
    }
    

    Now if you’re in Node.js you can reach the function like this:

    my_module = require('my_module_name');
    my_module.some_module_function(58);
    

    Or if you’re in a browser you can just call it directly since it’s global:

    my_module.some_module_function(58);
    

    Otherwise I can recommend Stitch.js which allows you to develop JavaScript code using the CommonJS style require and modules, then compile it to also run in the browser with no code changes.

    Require.js also allows for this type of functionality.

    Here’s another SO question about using the same code in node and browsers:
    how to a use client js code in nodejs

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

Sidebar

Related Questions

How can you use jQuery to include/require a different php file if window height
My project contains a lot of classes, a bunch of which can be described
I'm writing c++ project, which contains several classes. I created .h file named Position.h,
Here I have one file which contains some information, and I want to check
(PHP 5.3.6) I have a php file which contains simply this text - there
I have got a JAR file which contains Utility classes, that are used in
I can't get server side includes to work. I simply want to include one
Can I include a file from a zip file in PHP? For example consider
Possible Duplicate: get name of current PHP script in include file Can an included
How can I include an HTML file in another HTML file. Is there any

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.