javascriptnode.jsrequirejsamdefine

Using requirejs With amdefine in node.js Producing Undefined Errors


Running a simple server as:

requirejs = require("requirejs");
if (typeof define !== "function") {
    define = require("amdefine")(module);
}

requirejs.config({
    nodeRequire: require,
    baseUrl: __dirname + "/../.."
});

requirejs([
    "url"
], function (
    url
) {
    console.log(url.parse("http://user:pass@host.com:8080/p/a/t/h?query=string#hash"));
});

Produces the error:

TypeError: undefined is not a function at Url.parse (url.js:293:32)

However, if I remove the line define = require("amdefine")(module);, it all suddenly works fine. Since I would like to use amdefine in my project, how can I fix this?


Solution

  • Instead of changing the global variable define, you want to create a private variable of the same name in each file that you want to use it in. Instead of the line I had trouble with above, I should have put

    if (typeof define !== 'function') { var define = require('amdefine')(module) }