javascript – {} on return from require()

Question:

I'm creating a module, but when I go to use it, I get an empty object in require()

Codes below:

dev/index.js

 module.exports = () => { 'use strict'; const names = require('./names.js'), findSuperman = require('./find.js'); if ( findSuperman(names()) ) console.log('O superman está dentro de names'); else console.log('O superman não está aqui'); }

dev/names.js

module.exports = () => ['Barry Allen', 'Hal Jordan', 'Kara Kent', 'Diana Prince', 'Ray Palmer', 'Oliver Queen', 'Bruce Wayne', 'Wally West', 'John Jones', 'Kyle Rayner', 'Arthur Curry', 'Clark Kent'];

dev/find.js

module.exports = (values) => {
  'use strict';
  let foundSuperman = false;

  values.forEach(name => {
    if (name === 'Clark Kent')
      foundSuperman = true;
  });

  return foundSuperman;
}

Hence, when running browserify

browserify dev/index.js -o out.js

out.js

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
module.exports = (values) => {
  'use strict';
  let foundSuperman = false;

  values.forEach(name => {
    if (name === 'Clark Kent')
      foundSuperman = true;
  });

  return foundSuperman;
}

},{}],2:[function(require,module,exports){
module.exports = () => {
  'use strict';

  const names = require('./names.js'),
  findSuperman = require('./find.js');


  if ( findSuperman(names()) )
    console.log('O superman está dentro de names');
  else
    console.log('O superman não está aqui');

}

},{"./find.js":1,"./names.js":3}],3:[function(require,module,exports){
module.exports = () => ['Barry Allen', 'Hal Jordan', 'Kara Kent', 'Diana Prince', 'Ray Palmer', 'Oliver Queen', 'Bruce Wayne', 'Wally West', 'John Jones', 'Kyle Rayner', 'Arthur Curry', 'Clark Kent'];

},{}]},{},[2]);

So far, so good 🙂 However… I try to do a test and it doesn't happen :/

 'use strict'; const oi = require('./out.js'); console.log(oi); // O retorno é: {}

Answer:

Good Morning!

I believe you are learning ES6 and Browserify, right? I think I understand your doubts, so here we go:

The dev/names.js and dev/find.js files return functions using the arrow-function notation. The dev/index.js file also returns a function, however it is never executed! Try changing this file to only

  const names = require('./names.js'),
        findSuperman = require('./find.js');


  if ( findSuperman(names()) )
    alert('O superman está dentro de names');
  else
    alert('O superman não está aqui');

Run the browserify dev/index.js -o out.js in the terminal and add the generated script to an HTML file and open it in the browser. After doing this, you will notice that an alert was displayed with the message "Superman is inside names".

About Browserify, your goal is to write JS code using NodeJS modules (CommonJS) and then make it run in the browser.

Scroll to Top