Javascript 如何访问我的捆绑包?
我有一个导出单个变量的Javascript 如何访问我的捆绑包?,javascript,Javascript,我有一个导出单个变量的脚本: module.exports = { hello: "world" }; 我正在绑定browserify,然后在我的index.html 这是我的html文件: <!DOCTYPE html> <html> <head> <title>Hello World</title> <script type="text/javascript" src="./b
脚本
:
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>
你好,世界
console.log(hello);
我发现我的变量hello
未定义。我可以用我的开发工具看到bundle.js
,所以我知道它就在那里。为什么正文中的脚本
无法访问bundle.js
正在导出的变量
我在这里遗漏了什么?在
CMD
call中
browserify-r./bundle模块.js:bundle>bundle.js
bundle-module.js是您的原始模块代码
bundle module.js:bundle-冒号后面的“bundle”将在require调用中使用
bundle.js是browserify生成的代码
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>
你好,世界
var bundle=需要(‘bundle’);
log(bundle.hello);
试试“var hello=“world”在您的文件中,bundle.js
file locatedMybundle.js
与我的index.html
位于同一级别,我试图声明var hello='world'
,然后将其导出,但这没有帮助。您是否知道module.exports
用于Node.js,不适用于浏览器端JavaScript?