2

I'm requiring a library in NodeJS which has a self-invoking function, that results an error because it looks for an object which is not initialized at that moment .

I want to dynamically require this library when that object is initialized.

Is there any way to dynamically require/ load a library ?

This is the part of library required : https://github.com/sakren/node-google-maps/blob/develop/lib/Google.js#L5

Actually I want to require when the window object is present (client-side rendering).

So something like this :

'use strict';

var React = require('react');
var Map = require('./map.jsx');
var Common = require('../common/common');
var MapStatic = require('./map-static.jsx');

exports.type = function() {
    return 'map';
};

exports.jsx = function(data) {
    if (Common.isServerSide()) {
        return (<MapStatic data={data}/>);
    } else {
        return (
            <Map data={data}/>
        );
    }
};

exports.transform = require('./map-transform.js');

The reason the code looks weired is that I'm using react.

5
  • Is there some code you can show us to help better illustrate your problem? You should just be able to require the library after you initialize the object. Commented May 22, 2015 at 21:15
  • @Brennan code added. I want to the library to be require if and only if the else statement is the branch to go. Commented May 22, 2015 at 22:13
  • 1
    require() is just a normal function. You can call it inside an if statement if you want. I really don't follow what the question is here beyond that. Commented May 23, 2015 at 7:18
  • To make it simple to understand , let me rephrase the problem : Let's assume that you have 2 files. File A requires File B in an else section of an if-else block which is never reached. In File B there's a self-invoking function , which its only job is to throw an error. So, on the server side, NodeJS , regardless of this fact that requiring File B can never be reached , runs through File B and throws an error. Commented May 25, 2015 at 17:27
  • Have you considered forking the repository and making the function not self invoking? This would solve your issue I believe. Commented May 27, 2015 at 22:30

2 Answers 2

3

In nodeJS require can be used anywhere at anytime whithout much limitations AFAIK.

Which error is thrown once you require at runtime ? In your else branch.

Sign up to request clarification or add additional context in comments.

1 Comment

When map.jsx is required, there's a self-invoking function in it that throws an error (it checks for something and it throws an error, nothing wrong with that). What I'm saying is that even if we put the require statement in the else section it will require it , although it's not supposed to. How can I make it not require it when it doesn't reach that part of the else-if statement.
2

Try the following.

requires = {}
function getter(key) {
  if(!requires[key]){
    requires[key] = require(key)
  }
  return requires[key]
}

2 Comments

from what I know, it's not possible to require on run time.
I have used similar code in apps to dynamically load routes for express. Here is a working example bitbucket.org/benjar12/forensiqwrapper/src/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.