Browserify not working with module.exports, even with --standalone option

谁说我不能喝 提交于 2021-01-07 02:49:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!