Trying to do such a thing using RequireJS:

  1. There is a Script1.js script that performs some actions with data from a DB.js script.
  2. There is a Script2.js script that also performs some actions with data from a DB script and loads this script from script1.js .
  3. There is the DB script itself, which simply has variables with the necessary data.

Approximate piece of code:

 //Script1.js define(["DB", "Script2"], function (DB, Script2) { return function Script1() { console.log(DB); //выводит function () { }, всё круто this.script2 = new Script2(); } ); //Script2.js define(["DB"], function (DB) { return function Script2(cid) { console.log(DB); //выводит undefined, не то, что хотелось бы } }); //DB.js define(function () { //сюда код попадает единственный раз, когда RequireJS подключает его из Script1.js var DB = function () {}; DB.someData = { one: "two" }; return DB; }); 

For some reason, undefined comes to Script2.js in the DB variable. Those. such as the DB file was already loaded once, and RequireJS decided to just do nothing and not return. I am sure that he knows what he is doing, but I want to know how to get what I ask from him all the same?

DB is such a kind of "class" with static properties. In this implementation, while trying to solve the problem, I realized something that was now written if it worked as I thought, then each time a new DB constructor function would be returned, but I would like a link to the one created at the beginning.

In general, I tried to explain as I could.

Bottom line: comes undefined , although I expected a DB constructor function. If you want to help, but the problems are not understood, I will try to explain in more detail.

  • Something in the spirit of define (["Script2"]) and var DB = require ("DB"), it is in Script1, it was loaded because it depends on Script2 and it can be called synchronously. I understand the problem, it often arises, requirejs really knows what he is doing, I write with a comment because I haven’t worked with requirejs for a long time, it seems there is still some nuance, but right now it’s reluctant to check) - Duck Learns to Hide
  • But in general, I don’t see something with the eye so that this code doesn’t work, it usually happens if there is a circular dependency, and if there are many identical tails, it seems to work. - Duck Learns to Take Cover
  • Well, plus it happens that you forget to return something from the module, crookedly define ('smth', ...) instead of define (['smth') and the same kind of typos. - Duck Learns to Take Cover
  • Try to make a minimal reproducible example, for example, here in the snippet, or on plunkr so that you can run and see what is actually happening - Grundy

1 answer 1

With a hint from Duck Learning, Uma realized that the problem was a cyclical dependency. The example did not fully simulate the real situation in the project. As Grundy said, a reproducible example was needed, then it would be easier to understand. In general, the solution from the documentation: when undefined arrives during a circular dependency, I assign the following to this property directly:

 DB = require("data/db/DB"); 

Probably there is a better solution (at least to get rid of cyclical dependency), but so far in the process of learning I didn’t go deeply, I try not to get lost in a bunch of new information, so this solution is still quite suitable.