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]);
'use strict';
const oi = require('./out.js');
console.log(oi);
// O retorno é: {}