Javascript modules

Asked

Viewed 42 times

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;

Because the mistake happens?

1 answer

3


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

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

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

Module documentation in Google Devs

  • When running the script the following error appears "GET http://127.0.0.1/modulos/src/config 404 (Not Found)", then add the extension ". js" file the error some, now I wonder if it is possible to run import code without file extension?

  • Works without extension?

  • That’s what I want to know. It works?

  • 1

    I think not because the browser will understand that it is a folder

  • 1

    The extension that defines what type of file it is, and the browser interprets it differently depending on the extension. No extension is a folder

Browser other questions tagged

You are not signed in. Login or sign up in order to post.