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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:52:45+00:00 2026-05-28T01:52:45+00:00

There are some third party Javascript libraries that have some functionality I would like

  • 0

There are some third party Javascript libraries that have some functionality I would like to use in a Node.js server. (Specifically I want to use a QuadTree javascript library that I found.) But these libraries are just straightforward .js files and not “Node.js libraries”.

As such, these libraries don’t follow the exports.var_name syntax that Node.js expects for its modules. As far as I understand that means when you do module = require('module_name'); or module = require('./path/to/file.js'); you’ll end up with a module with no publicly accessible functions, etc.

My question then is “How do I load an arbitrary javascript file into Node.js such that I can utilize its functionality without having to rewrite it so that it does do exports?”

I’m very new to Node.js so please let me know if there is some glaring hole in my understanding of how it works.


EDIT: Researching into things more and I now see that the module loading pattern that Node.js uses is actually part of a recently developed standard for loading Javascript libraries called CommonJS. It says this right on the module doc page for Node.js, but I missed that until now.

It may end up being that the answer to my question is “wait until your library’s authors get around to writing a CommonJS interface or do it your damn self.”

  • 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-05-28T01:52:46+00:00Added an answer on May 28, 2026 at 1:52 am

    There is a much better method than using eval: the vm module.

    For example, here is my execfile module, which evaluates the script at path in either context or the global context:

    var vm = require("vm");
    var fs = require("fs");
    module.exports = function(path, context) {
      context = context || {};
      var data = fs.readFileSync(path);
      vm.runInNewContext(data, context, path);
      return context;
    }
    

    And it can be used like this:

    > var execfile = require("execfile");
    > // `someGlobal` will be a global variable while the script runs
    > var context = execfile("example.js", { someGlobal: 42 });
    > // And `getSomeGlobal` defined in the script is available on `context`:
    > context.getSomeGlobal()
    42
    > context.someGlobal = 16
    > context.getSomeGlobal()
    16
    

    Where example.js contains:

    function getSomeGlobal() {
        return someGlobal;
    }
    

    The big advantage of this method is that you’ve got complete control over the global variables in the executed script: you can pass in custom globals (via context), and all the globals created by the script will be added to context. Debugging is also easier because syntax errors and the like will be reported with the correct file name.

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

Sidebar

Related Questions

The TImage control doesn't support PNG. Is there some third party component that will?
My application uses some third-party libraries. I need to log some exceptions that occur
This warning is popping up a bunch in some third party libraries. Is there
Is there some way I can use URLs like: http://www.blog.com/team-spirit/ instead of http://www.blog.com/?p=122 in
I am using a third party library that provide some callbacks for a widget,
Does anyone know if there is some option in VS2010 or some third party
We have a third party library that internally uses a SafeHandle to an unmanaged
I have a site that is intended to be combined with third party skins
I am aware of some third party libraries and open source libraries to extract
Is there some rare language construct I haven't encountered (like the few I've learned

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.