简体   繁体   中英

How to integrate a browserified node module into brunch-built app?

New with front end dev, i'm trying to build a backbone based webapp with brunch. Everything's went fine until i tried to used a node module called node-xmpp-client . There's a browserified version of this module: node-xmpp-browser.js which works fine in my Google Chrome.

1- I tried to add this node-xmpp-browser.js to vendor dir to have it bundled into app.js but the following error occurs in my js console when loading the index.html page:

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- Then, i tried to add this script into my index.html page directly (as does the node-xmpp-client browser example):

<!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>

But the console complains again:

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

Seems that there's a conflict with how to declare a module (through require keyword) in a browserified script and a brunch built script?

What is the correct way to include such a browserified module into a brunch based app ?

Thanks a lot for your time,

Pierre

You need a require definition bundled into your client-side scripts. This comes by default in Brunch, so either the order of your script tags needs to be adjusted so that require is defined before any script that is expecting gets loaded, or you might be setting modules: definition: false in your config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM