Browserify“模块未定义”。 [英] Browserify "module is not defined"

查看:84
本文介绍了Browserify“模块未定义”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的浏览器。我尝试下面的代码,并在加载网页时出现 Uncaught ReferenceError:模块未定义。一切都很简单明了,所以不确定我在做什么:

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:

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>

用于浏览的命令:

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:

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


推荐答案

函数 parseChronoAndBuildElements 未定义在 getChronoOpenList 函数的范围内。

The function parseChronoAndBuildElements is not defined in the scope of your getChronoOpenList function.

您需要定义 parseChronoAndBuildElements 在该范围内,例如:

You need to define parseChronoAndBuildElements in that scope, like this:

chronoOpenList.js:

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

这篇关于Browserify“模块未定义”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆