2

I've got 3 files: index html :

<!DOCTYPE html>
  <html lang="en">
   <head>
     <meta charset="UTF-8">
   </head>
   <body>

    <script type="application/javascript" src="js/dtms.js"></script>
   </body>
  </html>

And 2 js files:

module:

 export const gg = ['123', '2343', '323'];

main js:

 import {gg} from "/path/main.js";

And I am getting error in chrome debugging console: Uncaught SyntaxError: Unexpected token {

How to fix?

4
  • 2
    <script type="module" src="path/to/file.js"></script> but take in mind that not all browser support modules yet, see caniuse.com/#search=module Commented Feb 12, 2018 at 19:38
  • some further infos to previous comment blog.whatwg.org/js-modules Commented Feb 12, 2018 at 19:43
  • @Yurii, so the only way is to use smth like require js to import and export modules? Commented Feb 12, 2018 at 20:05
  • @I.Gursky it depends on what you need. If you need to load each of your js modules asynchronously then yes AMD (require.js, system.js etc) could be used. But it doesn't mean you need to write your code in an old boring AMD way like define('module', [dep1, dep2], function() { /*code*/ }). You can use some tools that convert es6 code to es5+AMD style. For example, you could use webpack+babel pair. But don't know why you may need this. Even with http2 you probably need to bundle your modules into one/few files. Commented Feb 12, 2018 at 20:27

1 Answer 1

2

It works here: here https://plnkr.co/edit/3Fy4J3PyvSOBn1avMvg4?p=preview

// Supported:

import {foo} from 'https://jakearchibald.com/utils/bar.js';
import {foo} from '/utils/bar.js';
import {foo} from './bar.js';
import {foo} from '../bar.js';

// Not supported:

import {foo} from 'bar.js';
import {foo} from 'utils/bar.js';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.