JavaScript modules

2

I'm having the following error when trying to run a module in JavaScript:

  

Uncaught SyntaxError: Unexpected identifier

  • index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Módulos JavaScript</title>
    <script src="index.js"></script>
</head>
<body>
    <!-- BODY -->
</body>
</html>
  • index.js

import nome from './src/config';
console.log(nome);
  • src / config.js

const nome = 'ECMAScript 6'
export default nome;

Why the error happens?

    
asked by anonymous 23.06.2018 / 21:02

1 answer

4

To use .js as a module, you need to declare this in the script tag:

          ↓     ↓
<script type="module" src="index.js"></script>

Without this attribute, JavaScript returns error in import or export .

Module documentation in Google Devs

    
23.06.2018 / 21:22