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 filedev/index.js
? This kind of Browserify error happens to me when there are errors of lint / syntax in code.– Sergio
1- Is declared as
dev/index.js
. 2- I did what you said. But it occurred to me to receive{}
again :/– ivomarsan