1
I am trying to work with modules in Javascript and came across a problem when calling a function that is in the file main.js
for index.html
.
<!doctype html>
<html lang="en">
<head>
<title>ES modules</title>
<script type="module" src="./main.js"></script>
</head>
<body>
<script>
fnFuncionou()
</script>
</body>
</html>
That one main.js
imports a text variable from another file, the mods.js
.
import texto from './mod.js'
console.log(`Fora da função ${texto}`)
function fnFuncionou() {
console.log(`Dentro da função ${texto}`)
}
Below the code of mods.js
:
let texto = "funcionou!"
export default texto
When executing the index.html
, I get the message from console.log(
Outside of function ${text})
, which is out of function in the main.js
. However, the function fnFuncionou()
is not called, giving the following error message:
index.html:10 Uncaught ReferenceError: fnFuncionou is not defined
Could anyone tell me why this is happening? It is possible to call this function fnFuncionou()
without changing the file structure of the folder? I summarized the problem, because the files I am working have much more code.
Thank you very much!
– Saulo Alves