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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:51:12+00:00 2026-06-08T18:51:12+00:00

I have a JavaScript file which is not written for Node.js. It is actually

  • 0

I have a JavaScript file which is not written for Node.js. It is actually written by me, but this question may be more understandable if you think of it as a third-party library.

Normally, a Node.js module only exposes the module.exports object to external code. This requires the file to be written with Node.js in mind.

However, the file isn’t written for Node.js, yet I want to include it into a Node.js module. I can’t edit this file, since it will be used later, independently of Node.js.

I imagine I could pull it off by reading the file and calling eval on the contents, but is there a better way?

TL;DR

How can I include a .js file into a Node.js module, without rewriting it to use Node.js-specific code like exports?

P.S. The reason I want to use it in Node.js is for a few tests before it’s deployed elsewhere.

  • 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-08T18:51:15+00:00Added an answer on June 8, 2026 at 6:51 pm

    It depends on how the code is written on that file. If the file contains a class or a single object then you can do this:

    (function(global) {
    
        function MyClass() {
    
    
        }
    
        MyClass.prototype = {
                ...
        };
    
        if( typeof module !== "undefined" && module.exports ) {
            module.exports = MyClass;
        }
        else if ( global ) {
            global.MyClass = MyClass;
        }
    
    })(this);
    

    You can modify the global assignment to be a deeper namespace instead if required.

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

Sidebar

Related Questions

I have a sample.xhtml file which has few lines of javascript. This javascript will
I have a file called bbUI.js which contains this bit of JavaScript. Outside of
I have a javascript file which has some code somewhat like this: var Slide
I have written a program in Javascript which reads numbers from a file ,sorts
I have a javascript file that reads another file which may contain javascript fragments
I have a javascript file which has a function calling a server (url) to
I have a JavaScript file which has a variable as such: var ads =
I have a JavaScript file which is loaded up at the end of my
I have JavaScript code which copies the value of input file and paste it
I have written ashx which merges and remove white-spaces for javascript and css 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.