gpt4 book ai didi

javascript - 如何将浏览器化节点模块集成到 brunch 构建的应用程序中?

转载 作者:行者123 更新时间:2023-12-03 09:30:52 30 4
gpt4 key购买 nike

前端开发新手,我正在尝试构建一个基于主干的带有早午餐的网络应用程序。一切都很顺利,直到我尝试使用名为 node-xmpp-client 的节点模块。 。该模块有一个浏览器化版本:node-xmpp-browser.js,它在我的 Google Chrome 中运行良好。

1-我尝试将此 node-xmpp-browser.js 添加到 vendor 目录以将其捆绑到 app.js 中,但在加载 index.html 页面时,我的 js 控制台中出现以下错误:

Uncaught TypeError: require.register is not a function Uncaught
TypeError: require.register is not a function Uncaught TypeError:
loaderPath.indexOf is not a function

2-然后,我尝试将此脚本直接添加到我的index.html页面中(就像node-xmpp-client浏览器示例一样):

<!DOCTYPE html>
<html lang="en">
<head>
<title>My WebUI</title>
<link rel="stylesheet" type="text/css" href="app.css">
<meta name="viewport" content="width=device-width" initial- scale="1.0">
</head>
<body>
<div class='root-view'></div>
<script src="node-xmpp-browser.js"></script>
<script src='vendor.js'> </script>
<script src='templates.js'> </script>
<script src='app.js'> </script>
<script> require('initialize'); </script>
</body>
</html>

但是控制台再次提示:

Uncaught TypeError: require.register is not a function
Uncaught TypeError: require.register is not a function
Uncaught Error: Cannot find module 'initialize'

似乎在浏览器化脚本和 brunch 构建脚本中声明模块(通过 require 关键字)的方式存在冲突?

将此类浏览器化模块包含到基于早午餐的应用程序中的正确方法是什么?

非常感谢您的宝贵时间,

皮埃尔

最佳答案

您需要将 require 定义捆绑到客户端脚本中。这是 Brunch 中默认提供的,因此需要调整脚本标签的顺序,以便在加载任何需要加载的脚本之前定义 require,或者您可能要设置 modules:定义:在您的配置中为 false

关于javascript - 如何将浏览器化节点模块集成到 brunch 构建的应用程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31512655/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com