admin管理员组文章数量:1429092
I have a very simple module that I am bundling with Browserify. I want to use that bundle both in the browser as well as in node. In node, it works just fine if I require
the non-bundled module; however, if I require
the browserified bundle, require
returns an empty object. Here's a reproduction:
Simple module
function Foo(bar) {
this.bar = bar;
}
module.exports = Foo;
Test script
var Foo = require("./foo"); // not bundled with Browserify
var Foob = require("./foob"); // bundled with Browserify
console.log("Foo =", Foo);
console.log("Foob =", Foob);
Executed thusly
browserify foo.js -o foob.js
node foo-test.js
Output
Foo = function Foo(bar) {
this.bar = bar;
}
Foob = {}
You can see that Foo
(the non-bundled version) is the expected function but Foob
(the bundled version) is a sad and empty object.
So the question is why isn't the browserified module working in node?
Clarification: I'm using browserify to bundle my webapp and I use its paths options to simplify paths in my app's require statements and avoid relative path hell. However, I'm trying to use tap to do unit testing, but it doesn't seem to have a similar configuration feature. Because of this, trying to require non-bundled files when using tap causes everything to break.
I have a very simple module that I am bundling with Browserify. I want to use that bundle both in the browser as well as in node. In node, it works just fine if I require
the non-bundled module; however, if I require
the browserified bundle, require
returns an empty object. Here's a reproduction:
Simple module
function Foo(bar) {
this.bar = bar;
}
module.exports = Foo;
Test script
var Foo = require("./foo"); // not bundled with Browserify
var Foob = require("./foob"); // bundled with Browserify
console.log("Foo =", Foo);
console.log("Foob =", Foob);
Executed thusly
browserify foo.js -o foob.js
node foo-test.js
Output
Foo = function Foo(bar) {
this.bar = bar;
}
Foob = {}
You can see that Foo
(the non-bundled version) is the expected function but Foob
(the bundled version) is a sad and empty object.
So the question is why isn't the browserified module working in node?
Clarification: I'm using browserify to bundle my webapp and I use its paths options to simplify paths in my app's require statements and avoid relative path hell. However, I'm trying to use tap to do unit testing, but it doesn't seem to have a similar configuration feature. Because of this, trying to require non-bundled files when using tap causes everything to break.
Share Improve this question edited Nov 25, 2014 at 21:03 Justin Johnson asked Nov 25, 2014 at 10:44 Justin JohnsonJustin Johnson 31.3k7 gold badges66 silver badges89 bronze badges 9-
1
You don't
require
a Browserify-ied bundle file – benhowdle89 Commented Nov 25, 2014 at 10:54 - foob.js is bundled with browserify. – Justin Johnson Commented Nov 25, 2014 at 11:00
-
Yes, I can see that, but you're not supposed to use
require
with an already bundled module. – benhowdle89 Commented Nov 25, 2014 at 11:05 - Is it impossible then to use bundles built with browserify in node? – Justin Johnson Commented Nov 25, 2014 at 11:18
-
1
That's not the intended use of Browserify. Browserify was built to emulate Node's
require
but for browser environments. So to use Browserify within Node would be pletely ass-backwards. – benhowdle89 Commented Nov 25, 2014 at 11:29
2 Answers
Reset to default 9I found a way around this. The solution is to use browserify's --standalone
option when bundling. This will add the necessary module.exports statement in the bundled output.
You want to nest browserify bundles. In this case, ensure that your nested bundles actually have module.exports
defined.
For instance in the main file of your foob.js
bundle, be sure to return a function you can use externally (using module.exports
)
本文标签: javascriptrequire returns empty object when using browserifyStack Overflow
版权声明:本文标题:javascript - require returns empty object when using browserify - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745427367a2658175.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论