2014-02-05 230 views
11

我是browserify的新手。我試過下面的代碼,並加載我的網頁時得到Uncaught ReferenceError: module is not defined。一切都非常簡單明瞭所以不知道我在做什麼錯:Browserify「模塊未定義」

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> 

瀏覽的命令:

[email protected] /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]) 

回答

1

功能parseChronoAndBuildElements未在getChronoOpenList功能的範圍界定。

您需要在範圍界定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); 
} 
1

我相信你需要:

var parseChronoAndBuildElements = window.parseChronoAndBuildElements;