如何访问我的捆绑包

How do I get access to my bundle?

本文关键字:我的 访问 何访问      更新时间:2023-09-26

我有一个script,我正在导出单个变量:

module.exports = {
    hello: "world"
};

我正在与browserify捆绑,然后在我的index.html中消费捆绑

这是我html文件:

<!DOCTYPE html>
<html>
    <head>
        <title>Hello World</title>
        <script type="text/javascript" src="./bundle.js"></script>
    </head>
    <body>
      <script>
        console.log(hello);
      </script>
    </body>
</html>

我得到我的变量hello未定义。 我可以使用我的开发工具查看bundle.js,所以我知道它在那里。 为什么我在正文中的script无法访问bundle.js导出的变量?

我在这里错过了什么?

CMD调用

browserify -r ./bundle-module.js:bundle> bundle.js

捆绑模块.js是您的原始模块代码

bundle-module.js:bundle - 冒号后"bundle"是将在require调用中使用的内容

bundle.js 是浏览器生成的代码

HTML

<!DOCTYPE html>
<html>
    <head>
        <title>Hello World</title>
        <script type="text/javascript" src="./bundle.js"></script>
    </head>
    <body>
      <script>
        var bundle = require('bundle');
        console.log(bundle.hello);
      </script>
    </body>
</html>