Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module.exports and es6 Import

React with babel. I have this confusion with imports and module.exports. I assume babel when converting the ES6 code to ES5 converts the imports and exports to require and module.exports respectively.

If I export a function from one module and import the function in another module, the code executes fine. But if I export the function with module.exports and import using "import" the error is thrown at runtime saying it is not a function.

I cooked up an example.

// Tiger.js function Tiger() {     function roar(terrian){         console.log('Hey i am in ' +  terrian + ' and i am roaing');     };     return roar; }  module.exports = Tiger;  // animal.js import { Tiger } from './animals';  var animal = Tiger(); animal("jungle"); 

I used babel with preset es2015 to transcompile it. This gives me the following error

Uncaught TypeError: (0 , _animals.Tiger) is not a function

But if I remove the module.exports = Tiger; And replace it with export { Tiger }; It works fine.

What am I missing here??

EDIT: I am using browserify as the module bundler.

like image 411
Nani Avatar asked Dec 14 '15 23:12

Nani


People also ask

What is import and export in ES6?

With the help of ES6, we can create modules in JavaScript. In a module, there can be classes, functions, variables, and objects as well. To make all these available in another file, we can use export and import. The export and import are the keywords used for exporting and importing one or more members in a module.

Can you use module exports with import?

Modules can also contain a default export, using the default keyword. A default export will not be imported with curly brackets, but will be directly imported into a named identifier. Now the default value and named values are both available to the script.

What is import in ES6?

Introduction to ES6 import:The import statement is used to import modules that are exported by some other module. A module is a file that contains a piece of reusable code. The import modules are in strict mode whether it is declared or not.

Does ES6 support import?

ES6 modules are automatically strict-mode code, even if you don't write "use strict"; in them. You can use import and export in modules.


2 Answers

export { Tiger } would be equivalent to module.exports.Tiger = Tiger.

Conversely, module.exports = Tiger would be equivalent to export default Tiger.

So when you use module.exports = Tiger and then attempt import { Tiger } from './animals' you're effectively asking for Tiger.Tiger.

like image 93
Matt Molnar Avatar answered Sep 24 '22 10:09

Matt Molnar


If you would like to import:

module.exports = Tiger 

you may use following construction:

import * as Tiger from './animals' 

Then it will work.

Another option is changing the export as described by @Matt Molnar but it is only possible if you are the author of the imported code.

like image 29
jmarceli Avatar answered Sep 21 '22 10:09

jmarceli