{} at require() return

Asked

Viewed 140 times

3

I’m creating a module, but when I 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 :) But... I try to take a test and it doesn’t happen :/

'use strict';

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

// O retorno é: {}

  • Because you have the variable findSuperman = require('./find.js'); undeclared? Have you tried placing those two lines (requires) outside the function you are exporting? i.e., at the beginning of the file dev/index.js? This kind of Browserify error happens to me when there are errors of lint / syntax in code.

  • 1- Is declared as dev/index.js. 2- I did what you said. But it occurred to me to receive {} again :/

1 answer

1


Good morning!

I believe you are learning ES6 and Browserify, right? I think I understand your doubts, so let’s go:

The dev/Names.js and dev/find.js files return functions using notation Arrow-Function. The dev/index.js file also returns a function, but it never runs! 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');

Execute the command 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 "The 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.

  • Sorry I didn’t see it before, I was a little away. Anyway, I found it strange because I didn’t have the expected result even with IIFE. But in its own way it worked perfectly. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.