简体   繁体   English

module.exports“模块未定义”

[英]module.exports “Module is not defined”

So, I am using RequireJS and React, trying to load a third-party component, which has been installed with: 所以,我正在使用RequireJS和React,尝试加载第三方组件,该组件已安装:

npm install react-autocomplete

The structure is here: https://github.com/rackt/react-autocomplete/tree/master/lib 结构在这里: https//github.com/rackt/react-autocomplete/tree/master/lib

Now, I have a main.js file, initiated when requireJS is loaded, that looks like this: 现在,我有一个main.js文件,在加载requireJS时启动,如下所示:

require.config({
paths: {
      "react" : "react/react",
      "jsx-transformer" : "react/JSXTransformer",
      "react-autocomplete" : "node_modules/react-autocomplete/lib/main"
    }
});

require(["react"], function(react) {
    console.log("React loaded OK.");
});

require(["jsx-transformer"], function(jsx) {
    console.log("JSX transformer loaded OK.");
});

require(['react-autocomplete'], function (Autocomplete) {
    console.log("React autocomplete component loaded OK.");
    var Combobox = Autocomplete.Combobox;
    var ComboboxOption = Autocomplete.Option;
    console.log("Autocomplete initiated OK");
 });

Now, it all loads OK, but the third require statement throws a "module is not defined", for the main.js file in the third-party component, which looks like this: 现在,它都加载正常,但第三个require语句为第三方组件中的main.js文件抛出“模块未定义”,如下所示:

module.exports = {
  Combobox: require('./combobox'),
  Option: require('./option')
};

I've been reading about that this has to do with me trying to require a CommonJS-style module, but I can't figure out how to fix it on my own, as I'm new to this. 我一直在阅读这与我试图要求一个CommonJS风格的模块有关,但我无法弄清楚如何自己修复它,因为我是新手。

Does anyone have a clear example on how I could get around this? 有没有人有一个明确的例子,我怎么能解决这个问题?

RequireJS cannot load CommonJS modules as-is. RequireJS无法按原样加载CommonJS模块。 However, there is a minimal modification you can make to them to load them. 但是,您可以对它们进行最小程度的修改以加载它们。 You have to wrap them in a define call like this: 你必须将它们包装在这样的define调用中:

define(function (require, exports, module) {

  module.exports = {
    Combobox: require('./combobox'),
    Option: require('./option')
  };

});

If you have a bunch of modules you need to convert, or if you are using a third-party library written in the CommonJS pattern and want to convert it as part of a build process, you can use r.js to perform the conversion for you. 如果您需要转换一堆模块,或者如果您使用的是以CommonJS模式编写的第三方库并希望将其转换为构建过程的一部分,则可以使用r.js执行转换您。

The problem is that requireJS doesn't support CommonJS modules only AMD. 问题是requireJS不支持只有AMD的CommonJS模块。 So if the third party library doesn't support AMD then you'll have to jump through some hoops to get it to work. 因此,如果第三方库不支持AMD,那么你将不得不跳过一些环节来让它工作。

If you have the option I would suggest using browserify or webpack for module loading since those are the tools that the majority of the react community have chosen and practically all third-party react components are published on npm as CommonJS modules. 如果有我会建议使用该选项browserify的WebPack的,因为这些模块加载是,大部分反应社会选择的工具和几乎所有的第三方反应组件在故宫作为CommonJS的模块发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM