I am new to browserify. I tried the code below and got Uncaught ReferenceError: module is not defined
when loading my web page. Everything is pretty plain and simple so not sure what I am doing wrong:
chronoOpenList.js:
module.exports = function getChronoOpenList() {
var xml = new XMLHttpRequest();
xml.open("GET", "api/nextrequestdue/", true);
xml.onreadystatechange = function () {
if (xml.readyState === 4 && xml.status === 200) {
var jsonText = xml.responseText;
parseChronoAndBuildElements(jsonText);
}
}
xml.send(null);
}
main.js:
var getChronoOpenList = require('./chronoOpenList');
getChronoOpenList();
html:
<script style="text/javascript" src="{% static 'js/bundle.js' %}"></script>
The command to browserify:
one@chat-dash /home/git/recognizer/recognizer_project/static/js $ /usr/local/lib/node_modules/browserify/bin/cmd.js main.js -o bundle.js
The bundle.js:
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
module.exports = function getChronoOpenList() {
var xml = new XMLHttpRequest();
xml.open("GET", "api/nextrequestdue/", true);
xml.onreadystatechange = function () {
if (xml.readyState === 4 && xml.status === 200) {
var jsonText = xml.responseText;
parseChronoAndBuildElements(jsonText);
}
}
xml.send(null);
}
....
},{"./chronoOpenList":1}]},{},[2])
Bundle up your first module With Browserify you can write code that uses require in the same way that you would use it in Node. Browserify parses the AST for require() calls to traverse the entire dependency graph of your project. Drop a single <script> tag into your html and you're done!
Browserify is an open-source JavaScript bundler tool that allows developers to write and use Node. js-style modules that compile for use in the browser.
Browserify solves the problems of having too many JS files referenced in your HTML, inability to use Node modules in the browser, and inability to reference your own modules in your own code. Watchify streamlines the process of bundling your files and will make a change every time you change a JS file in your project.
What is Browserify? Browserify allows us to use node. js style modules in the browser. We define dependencies and then Browserify bundles it all up into a single neat and tidy JavaScript file.
The function parseChronoAndBuildElements
is not defined in the scope of your getChronoOpenList
function.
You need to define parseChronoAndBuildElements
in that scope, like this:
chronoOpenList.js:
var parseChronoAndBuildElements = require('./parseChronoAndBuildElements.js');
module.exports = function getChronoOpenList() {
var xml = new XMLHttpRequest();
xml.open("GET", "api/nextrequestdue/", true);
xml.onreadystatechange = function () {
if (xml.readyState === 4 && xml.status === 200) {
var jsonText = xml.responseText;
parseChronoAndBuildElements(jsonText);
}
}
xml.send(null);
}
I believe you need to:
var parseChronoAndBuildElements = window.parseChronoAndBuildElements;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With