问题
I have the following JS file:
common.js
:
"use strict";
const MarketingPolicy = {
Subscriptions: {
// Some properties here.
},
Campaigns: [
// Some objects here.
]
};
const otherObject = {
// Some properties here.
};
module.exports = {
otherObject,
MarketingPolicy
};
This works in Node.js:
const common = require("./common.js");
const policy = common.MarketingPolicy; // No error.
But when exporting it to common.bundle.js
with Browserify, none of the objects is accessible at the client-side (undefined
), even with the --standalone
option:
None of the following is working:
browserify functions/middleware/common.js | uglifyjs > store/public/app/scripts/common.bundle.js
browserify functions/middleware/common.js > store/public/app/scripts/common.bundle.js
browserify functions/middleware/common.js --s common > store/public/app/scripts/common.bundle.js
browserify functions/middleware/common.js --standalone common > store/public/app/scripts/common.bundle.js
How did I test it at client-side?
In app.html:
<script src="/app/scripts/common.bundle.js"></script>
<script src="/app/scripts/app.js"></script>
In app.js:
const policy = MarketingPolicy; // undefined
const policy = common.MarketingPolicy; // common is undefined.
Use global
?
When I use global
in my common.js
, the properties in the MarketingPolicy
object no longer accessible at both server and client-side:
global.MarketingPolicy = {
Subscriptions: {
// Some properties here.
},
Campaigns: [
// Some objects here.
]
};
module.exports = {
otherObject,
MarketingPolicy: this.MarketingPolicy
};
In Node.js:
const common = require("./common.js");
const subscriptions = common.MarketingPolicy.Subscriptions; // Subscriptions is undefined.
This problem took me whole day to troubleshoot and research without making my common.js
works for both ends. Trust me, I even put all my objects in one single module.exports
and still not working. I saw many people mark the --standalone
option as answer on many other posts -- Have I missed something else in my codes? Hope someone can point it out to me. Thank you so much in advance!
来源:https://stackoverflow.com/questions/65406191/browserify-not-working-with-module-exports-even-with-standalone-option