8

I have this module that consists of AJAX functionality I'm looking to export to my Client Controller, but I'm getting Module is not defined, I'm using NodeJS and the Cloud9 Environment.

AJAX Module

module.exports = {
  appUrl: window.location.origin,
  ready: function(fn) {
    if(typeof fn !== 'function') {
        return;
    }

    if(document.readyState === 'complete') {
        return fn();
    }

    document.addEventListener('DOMContentLoaded', fn, false);
},
  ajaxRequest: function(method, url, callback) {
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState === 4 && xmlhttp.status === 200) {
            callback(xmlhttp.response);
        }
    };

    xmlhttp.open(method, url, true);
    xmlhttp.send();
  }
};

Client Controller

'use strict';

 var ajaxFunctions = require("../common/ajax-functions.js");

 (function(){

   var profileId = document.querySelector('#profile-id') || null;
   var profileUsername = document.querySelector('#profile-username') || null;
   var profileRepos = document.querySelector('#profile-repos') || null;
   var displayName = document.querySelector('#display-name');
   var apiUrl = ajaxFunctions.appUrl + '/api/:id';

   function updateHtmlElement(data, element, userProperty) {
       element.innerHTML = data[userProperty];
   }

   ajaxFunctions.ready(ajaxFunctions.ajaxRequest('GET', apiUrl, function(data){
       var userObject = JSON.parse(data);

       updateHtmlElement(userObject, displayName, 'displayName');

       if(profileId !== null) {
           updateHtmlElement(userObject, profileId, 'id');
       }

       if(profileUsername !== null) {
           updateHtmlElement(userObject, profileUsername, 'username');
       }

       if(profileRepos !== null) {
           updateHtmlElement(userObject, profileRepos, 'publicRepos');
       }
   }));
})();
6
  • Does the error message give any indication as to on which line the error occurs? Just a guess, I think you should check var ajaxFunctions = require("../common/ajax-functions.js"); points to the correct location, and that ajax-functions.js exports a valid module. Commented Jan 5, 2017 at 21:53
  • Will do thanks, I'm getting an error on line 1 of the ajax-functions.js Commented Jan 5, 2017 at 21:57
  • Could you post that line? Commented Jan 5, 2017 at 21:59
  • Uncaught ReferenceError: module is not defined at ajax-functions.js:1 Commented Jan 5, 2017 at 22:01
  • I mean line 1 of ajax-functions.js :) Commented Jan 5, 2017 at 22:03

2 Answers 2

5

It seems like you're trying to run the code in a browser, and the error you're getting is saying that module is not defined. If this code is intended to be run in a browser, you'll have to package it with Webpack or Browserify first.

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

Comments

0

Node doesn't have its own XmlHttpRequest, which is something we take for granted in the web/browser world. This is likely the cause of the errors in your AJAX module. It is trying to reference a variable (XmlHttpRequest) which doesn't exist, and since it doesn't exist the file gets an error.

You could fix this by bringing in your own XmlHttpRequest, such as this one: https://github.com/driverdan/node-XMLHttpRequest. To do this simply run:

npm i --save node-XMLHttpRequest

However, I'd recommend instead bringing in a fetch. fetch is the new, cool, non-jQuery but still easy way of making AJAX requests, and it's supported by most browsers (http://caniuse.com/#search=fetch). Just as with XmlHttpRequest, Node doesn't have its own version of fetch, but you can easily add it with:

npm i --save whatwg-fetch

The benefit of using fetch is that you'll have less "re-inventing of the wheel" to do vs. XmlHttpRequest. You can learn more about fetch here: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

Comments

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.