Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

npm install react-autocomplete 

The structure is here: 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:

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:

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.

Does anyone have a clear example on how I could get around this?

like image 656
joakimnorberg Avatar asked Apr 15 '15 14:04

joakimnorberg


People also ask

How do you fix module is not defined?

The error "Module is not defined in ES module scope" occurs when we try to use the module. exports CommonJS syntax in ES modules. To solve the error, use the export keyword to export a member from a file, e.g. export const num = 42 .

Is not defined by exports?

To solve the "Uncaught ReferenceError: exports is not defined", add a script tag that defines an exports variable, e.g. <script>var exports = {};</script> above your JS script tag if in the browser, or remove the type attribute if set to module in your package.

What is module exports in node?

Module exports are the instructions that tell Node. js which bits of code (functions, objects, strings, etc.) to export from a given file so that other files are allowed to access the exported code.


2 Answers

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

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.

like image 53
Louis Avatar answered Sep 20 '22 11:09

Louis


The problem is that requireJS doesn't support CommonJS modules only AMD. So if the third party library doesn't support AMD then you'll have to jump through some hoops to get it to work.

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.

like image 24
Brandon Pugh Avatar answered Sep 18 '22 11:09

Brandon Pugh