0

The following code works:

define(["./my/dependency"], function (myDep) {
    return awesomeness
    // Everything is fine :)
}

It would be very nice, due to restrictions in the environment I'm working in, to define dependencies earlier in an array but this doesn't work:

deps = ["./my/dependency"]

define(deps, function (myDep) {
    return abandonAllHope
    // Everything is terrible :(
})

I'm new to Javascript/requireJS/nodeJS and working to alter an existing nodeJS project. The website fails to load major elements when I try the second option. I'm not sure how to bugfix this issue and can't understand why only the second option fails.

kuro
  • 1
  • 2
  • How are you adding the dependencies to the array? – ibrahim mahrir May 15 '18 at 01:17
  • Right now just as shown, with an implicitly created array of strings. (`deps = ["./my/dependencies"]`) I was trying to do something fancier earlier, but then boiled the error down to this. – kuro May 15 '18 at 01:26
  • [**That doesn't seem to be the error**](https://stackoverflow.com/q/12047637/). There is probably something else wrong somewhere. – ibrahim mahrir May 15 '18 at 01:35

1 Answers1

0

I can see two problems:

  • missing var keyword
  • you are defining global variable deps which might be overwriting something

Please try this one, this uses anonymous function which is calling itself instance:

(function () {
    var deps = ["./my/dependency"];

    define(deps, function (myDep) {
        return abandonAllHope
        // Everything is terrible :(
    });
}());
Damian Dziaduch
  • 2,107
  • 1
  • 15
  • 16