module.exports" Module is not defined" [英] module.exports "Module is not defined"
问题描述
所以,我正在使用RequireJS和React,尝试加载第三方组件,该组件已安装:
So, I am using RequireJS and React, trying to load a third-party component, which has been installed with:
npm install react-autocomplete
结构在这里: https://github.com/rackt/react-autocomplete/tree/master/lib
现在,我有一个main.js文件,在加载requireJS时启动,如下所示:
Now, I have a main.js file, initiated when requireJS is loaded, that looks like this:
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");
});
现在,所有加载都正常,但第三个require语句抛出模块未定义,对于第三方组件中的main.js文件,如下所示:
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:
module.exports = {
Combobox: require('./combobox'),
Option: require('./option')
};
我一直在读这与我试图要求一个CommonJS式模块有关,但我无法弄清楚如何自行修复它,因为我是新手。
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.
有没有人有一个明确的例子说明我怎么能得到它围绕这个?
Does anyone have a clear example on how I could get around this?
推荐答案
RequireJS无法按原样加载CommonJS模块。但是,您可以对它们进行最小程度的修改以加载它们。你必须将它们包装在 define
这样的调用中:
RequireJS cannot load CommonJS modules as-is. 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(function (require, exports, module) {
module.exports = {
Combobox: require('./combobox'),
Option: require('./option')
};
});
如果您有一堆模块需要转换,或者您使用的是第三方以CommonJS模式编写的库,并希望将其转换为构建过程的一部分,您可以使用 r.js
为您执行转换。
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.
这篇关于module.exports" Module is not defined"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!