1
votes

I have a module (dependencyA) that I want to call from two separate modules (modA & modB) while using RequireJS. In this case, modA is the main.js file. I have mentioned the path of the dependency in the path section of main.js:

    paths: {
        'dependencyA': 'test/dependencyA',
//other paths
}

I added this dependency in my define function (I am using define and not require in the main.js) - I removed references to other modules for this post:

define(['dependencyA'], function (dependencyA) {

I am able to use this dependency from the main.js file. If I wish to use this dependency from moduleB, should I add it over there in the define section or not? If I add it in the define section, my requireJS module code does not seem to be correct (my javascript code does not get called properly). I do not think the dependency code is incorrect since its being called from the main.js file fine.

This is what I tried in moduleB:

define(["require", './test/dependencyA'], 
        function(require, dependencyA) { 

If I remove the reference to dependencyA from moduleB, I get the error stating that dependencyA is undefined.

The dependency module dependencyA is defined as follows (I did not pass in the name):

define(function() {

Any suggestions on fixing this will be greatly appreciated.

1
You should be able to use same module in as many places as you like. Everything looks fine in your sample. Are you sure you are setting up paths correctly? I suggest you to specify baseUrl and then reference modules using url relative to baseUrl. - Tomas Kirda
thanks for the comment and answer - just want to make sure that I communicate all related info. My folder structure is as follows: main.js in parent directory, modB in a child directory 1, dependency in another child directory called test (from parent, not not from the first child directory). I am calling dependency in test directory from modB and am trying to use this syntax: './test/dependencyA' - this works for other modules (not imported in main.js) but does not work for me when I imported dependency in main.js as well. Hope it provides more info. - ali haider
Not sure what you mean by "imported in main.js". In main.js just use same methodology, why need to specify path alias then? - Tomas Kirda

1 Answers

1
votes

If you set path for a module, just reference it by that alias:

define(["require", 'dependencyA'], 
        function(require, dependencyA) {