Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module is not defined in NodeJS

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');
       }
   }));
})();
like image 972
Lewis Briffa Avatar asked Jan 05 '17 21:01

Lewis Briffa


People also ask

How do you fix the error require is not defined in node JS?

You are using require in a Node. If that is set to module , ES6 modules will be enabled and you will run into the error mentioned above (specifically ReferenceError: require is not defined in ES module scope, you can use import instead ). Simply remove that entry and you will be able to use require .

Is not defined in node JS?

The "ReferenceError: path is not defined" occurs when we use the path module without importing it in a Node. js application. To solve the error, make sure to import the path module before using it - import path from 'path' . To solve the error, import the path module before using it.

Which module is not built in node JS?

Explanation: The fsread modules is not a built-in node module in Node. js.

How do I fix a Node module error?

To fix Cannot find module errors, install the modules properly by running a npm install command in the appropriate directory as your project's app. js or index. js file. or delete the node_modules folder and package-lock.


1 Answers

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.

like image 183
Schlaus Avatar answered Sep 21 '22 11:09

Schlaus