Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does JS import always requires export?

I spent a little more time in Python and there, importing from another file just takes a
from file import functionName Is export required in Javascript when importing? I would like to split my single file into several different ones as it becomes more and more difficult to read. There should be only one function in each of the additional files. When I tried to import these extra files just by import {functionName} from './modules/filename.js' then I was getting Uncaught SyntaxError: The requested module './modules/filename.js' does not provide an export named 'functionName', but when I added when I wrote export before the function like export function functionName() {}, everything works fine. Do I understand correctly that in JS, import always requires export?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Your JS file is a module and you need to specify what will be exposed by this module:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

You could create a library where you do not want to expose all internal methods but only some of them and you can achieve that with the export keyword.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading