0
Answered

Es6 Modules

codeCaveMan 6 years ago updated by Alexander Blach (Developer) 6 years ago 4

Does testastic support importing and exporting modules between js files? For some reason, the following code doesn’t seem to work https://feedback.textasticapp.com/communities/1-textastic-for-ios#

// file1.js

function sum(num1, num2) { 

     return num1 + num2;

}

export default sum;

// file2.js

import sum from "./file2.js"; 

console.log(sum(1, 2)); // instead of 3 I get the message “SyntaxError: Unexpected identifier 'sum'. import call expects      
exactly one argument.”

note: file1 and file2 are in the same folder.

Thanks for your help.

Errol

ps, I am running the main.js file from an index.html file. 

Under review

Hello,

you need to make sure that you use type="module" in the script tag in the html file, like this:

<script src="file2.js" type="module"></script>

Also, it should probably be 

import sum from "./file1.js"; 

in the first line of file2.js.

+1

It works! Thanks for a great app and your excellent surpport.