9

I am trying to import a function from another javascript file but an error occurs with the import statement in main.js.

main.js:

import {Event} from 'event.js';

let asdf = new Event("hi", "hi", "hi");
console.log(asdf.title);
console.log(asdf.mainText);
console.log(asdf.buttonSet);

event.js:

export function Event(title, mainText, buttonSet) {
    this.title = title;
    this.mainText = mainText;
    this.buttonSet = buttonSet;
}

I looked up the syntax and don't see anything wrong:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

Also, I ran the code snippet in this link and got the same error. ES6 in the browser: Uncaught SyntaxError: Unexpected token import

Edit: Corrected index.html file:

<script src="scripts/main.js" type="module"></script>
3
  • 1
    Are you using a transpiler to handle the es6 syntax? (es6 syntax meaning the import and export statements) Commented Jun 19, 2018 at 23:38
  • Sorry, I am new to javascript. What is a transpiler? Commented Jun 19, 2018 at 23:45
  • oh, so you already have type="module" ... well, that's interesting. WHich browser and browser version? Commented Jun 20, 2018 at 0:02

3 Answers 3

18

Based on the error message

  1. you're running Chrome,
  2. you haven't set type="module" in the script tag

I'm trying to find some good documentation regarding module type for script tag, however this is the best I can find so far

So, if your script tag is

<script type="application/javascript">
import {Event} from "event.js";
......
</script>

or even if it is the following (because type is optional)

<script>
import {Event} from "event.js";
......
</script>

change it to

<script type="module">
import {Event} from "event.js";
......
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

Where should I add that?
Sorry, was adding a link ... <script type="module" ..... so, instead of <script type="application/javascript" .... ( or, of course, type is actually optional now for javascript scripts)
@JaromandaX what if I am not using HTML. Where should I add the script tag?
@coder123 - 1. if you are "not using HTML" - then there's nowhere to put the html SCRIPT tag. 2 - how is that relevant to this question and answer?
Because I am getting the same error but I don't know how to solve it !
3

Your syntax is fine if you're using a transpiler, specifically babel is the transpiler most people use these days.

You can specifically enable some es6 functionality (including import and export) in modern browsers using the "module" type in your script <script type=module src="main.js" /> but if you're running locally cross origin request stuff will not cooperate, so you can host your code locally using a web server like express.js

There are many many tutorials available online giving you a rundown on how to begin with web development in the modern age, but here's a list of topics you can research yourself.

  1. node.js This is for javascript execution
  2. express.js This is for creating servers using node.js
  3. es6 This is a specification, a newer "version" of ecmascript (javascript), it has new features like import and export and object destructuring
  4. babel This is a transpiler, something that converts new code (such as es6) into code that can run on older systems (such as es5)
  5. webpack This is a javascript packaging system, used to combine code and execute modules like transpilers

There's a lot to learn, and one of the best places that we as a community can improve. To avoid all of the messy startup stuff you can try a project like https://codesandbox.io/ which aims to make this whole process much easier.

1 Comment

"express.js This is for creating servers using node.js" should be stated that this is not required to create a server, it could be called a 'luxury' or non-essential item just as jquery is not required to use javascript.
0

In your export

Export default function Event(title,mainText,buttonSet){
    this.title=title;
    this.mainText=mainText,
    this.buttonSet=buttonSet;
}

And in your import

import Event from "event.js";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.