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

Javascript how to use modules to Import/Export all from file?

With this code I am able to successfully import / access variables and functions when they are individually exported, but I want to be able to export the entire file in a single declaration. I’ve tried a number of variations from here but cannot achieve this.

script.js

import * as testImport from './test.js';

console.log( testImport.testVar ); // Returns 'Test Variable'
console.log( testImport.testFunc() ); // Returns 'Test Function'

test.js

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

export let testVar = 'Test Variable';

export function testFunc() {
    return 'Test Function';
}

desired ( without individual ‘exports’ )

let testVar = 'Test Variable';

function testFunc() {
    return 'Test Function';
}

export * // ?

>Solution :

There is no such thing like you ask for, because it conflicts diametrically with the intention behind modules.

The whole point of modules is too keep all variables private to the module with the exception of making those explicitly exported available for importing.

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