14

I want to be able to inject extra paths, in a file different from the one which contains the config. Can this be done? A bonus question is whether I can directly access "config" variables.

1
  • 2
    I understand you ask if it possible to add additional paths after require.config() has been run? Commented Sep 30, 2013 at 8:57

1 Answer 1

18

There is no problem with calling require.config multiple times or from multiple places. You don't have to provide an entire set of configuration on subsequent calls. The new path mappings will be merged with existing ones.

For example, if you did this originally:

require.config({
    paths: {
        foomodule: 'libs/foo',
        jquery:  'libs/jquery'
    }
});

You could later do this to provide a different set of paths for jquery and/or to inject paths for a whole new module not present in the original config:

require.config({
    paths: {
        jquery:  [ 'http://code.jquery.com/jquery-2.0.2', 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.2' ],
        // note that foomodule not provided here but still keeps its original configuration
        someothermodule: 'some/other/path'
    }
});

Note, however, that if a module was already loaded based on the original config and you wanted to force it to reload from the new config you might have to call require.undef

Regarding the 2nd part of your question (reading the existing config information), I asked a question on this too and so far have not found a way to do it.

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

2 Comments

for me it doesnt work. can you please provide example with details.?
It works. For the issue with the optimizer, you just have to add manually those files with include option.

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.