React with babel. I have this confusion with import and module.exports. I assume that babel when converting ES6 code to ES5 will convert the import and export to the required and module.exports respectively.
If I export a function from one module and import a function into another module, the code will execute perfectly. But if I export a function using modules.exports and import using "import", an error is thrown at runtime, saying that it is not a function.
I have prepared 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 preinstalled es2015 to recompile it. This gives me the following error:
Uncaught TypeError: (0, _animals.Tiger) is not a function
But if I remove module.exports = Tiger; And replace it with export { Tiger }; It works great.
What am I missing here?
EDIT: I use browserify as a bundler module.
source share