Webpack模块RefrenceError模块未定义

时间:2017-06-07 21:21:17

标签: javascript webpack module-pattern

我正在使用webpack2捆绑一些模块。模块使用通用模块模式(不是CommonJS或AMD)编写。

// in spUtil.js
var spUtil = (function(){
  var sp={};
  /* build some helper functions to set up urls and rest queries for sharepoint */
  return sp;
})();


// in listcrud.js
var listCrud=(function(){
  lc={};
  /* build some functions that get data from sharepoint lists */
  return lc;
})();

// in util.js
require("exports-loader?spUtil!./spUtil.js");
require("exports-loader?listCrud!./listcrud.js");
var util=(function(spUtil,listCrud){
  ut={};
  /* build some functions that query data from sharepoint */
  return ut;
})($,spUtil,listCrud);

// in myModule.js
var myModule=(function($,util){
  my={};
  /* build a view of some SP data */
  return my;
})($,util);

但是当webpack尝试加载该包时,我收到错误;

Uncaught ReferenceError: spUtil is not defined
    at Object.$.ajax.url (dashboard.bundle.js:389)
    at __webpack_require__ (dashboard.bundle.js:20)
    at Object.<anonymous> (dashboard.bundle.js:1076)
    at __webpack_require__ (dashboard.bundle.js:20)
    at Object.<anonymous> (dashboard.bundle.js:813)
    at __webpack_require__ (dashboard.bundle.js:20)
    at Object.<anonymous> (dashboard.bundle.js:1729)
    at __webpack_require__ (dashboard.bundle.js:20)
    at dashboard.bundle.js:66
    at dashboard.bundle.js:69

Webpack构建如下:

/* 0 */
/***/ (function(module, exports, __webpack_require__) {

__webpack_require__(2);
__webpack_require__(1);
var util =(function($,spUtil,listCrud){
....
})($,sqUtil,listCrud);

/* 4 */
/***/ (function(module, exports, __webpack_require__) {

__webpack_require__(0);
var myModule=(function($,util){
  var my={};
  .....
  return my;
})($,util);

其中模块2是spUtil,模块1是listCrud。单步执行dev工具调试器 - 加载spUtil,listCrud已经被webpack“安装”了这个函数被调用:

/******/        if(installedModules[moduleId]) {
/******/            return installedModules[moduleId].exports;
/******/        }

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

我想出了我的错误,我错过了一个关键组件。在模块中声明变量以供其使用。

var spUtil = require('...spUtil.js');
var listCrud = require('...listcrud.js');