{} on return from require ()

3

I'm creating a module, but when I'm going 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;
}

Then, when you run the 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. But ... I try to make a test and it does not roll: /

'use strict';

const oi = require('./out.js');
console.log(oi);

// O retorno é: {}
    
asked by anonymous 12.04.2016 / 17:06

1 answer

1

Good morning!

I believe you are learning ES6 and Browserify, right? I think I understood your doubts, so come on:

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, but it never executes! 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 command on 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 within names".

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

    
03.06.2016 / 16:33