如何访问我的捆绑包?

时间:2015-01-08 21:26:58

标签: javascript

我有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正在导出的变量?

我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

CMD电话

  

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

bundle-module.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>
相关问题