Browserify“模块未定义”

我是新的浏览器。 我试了下面的代码,并得到Uncaught ReferenceError: module is not defined加载我的网页时Uncaught ReferenceError: module is not defined 。 一切都很简单,所以不知道我在做什么错:

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> 

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 

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]) 

Solutions Collecting From Web of "Browserify“模块未定义”"

函数parseChronoAndBuildElements没有在getChronoOpenList函数的作用parseChronoAndBuildElements定义。

您需要在该范围内定义parseChronoAndBuildElements ,如下所示:

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); } 

我相信你需要:

 var parseChronoAndBuildElements = window.parseChronoAndBuildElements;