0

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

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 * // ?
Toki
  • 517
  • 3
  • 9
  • 27
  • Can you clarify what you mean by "all from file"? I understand the description of your problem as "How do you export in a single statement instead of in individual statements?" Though well [explained on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export), I see why this can be confusing. – Oskar Grosser Dec 01 '21 at 12:30
  • I'm still familiarizing myself with the correct usage of modules so really was just exploring whether they can be used in the same way an ajax or getscript request would be, but as mentioned below this isn't what they're meant for. – Toki Dec 01 '21 at 12:39

1 Answers1

1

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

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

connexo
  • 53,704
  • 14
  • 91
  • 128
  • I'll add for anyone looking for this functionality, this is covered comprehensively here -- https://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file – Toki Dec 01 '21 at 12:28
  • You think your question is a candidate for being a duplicate to that question? – connexo Dec 01 '21 at 12:30
  • I don't think so since it pertains specifically to the usage of modules, albeit as you say their function is different to what I am after. Appreciate your clarification on this - thanks! – Toki Dec 01 '21 at 12:41