# Unexpected Token "import" in script

**URL:** <https://discourse.mapeditor.org/t/unexpected-token-import-in-script/7690>\
**Category:** Questions\
**Created:** [November 9, 2025, 3:54am UTC](https://discourse.mapeditor.org/t/unexpected-token-import-in-script/7690 "2025-11-09T03:54:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sancho](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@sancho](https://discourse.mapeditor.org/u/sancho)\
**Post date:** [November 9, 2025, 3:54am UTC](https://discourse.mapeditor.org/t/unexpected-token-import-in-script/7690/1 "2025-11-09T03:54:30Z")

</div>

I am trying to load an .mjs file

`import {msg} from “./general.mjs”;`

and am getting the error

```auto
SyntaxError: Unexpected token `import'

Stack traceback:

  @file:///home/les/Documents/act-fast/Assets/Ignored/TiledProject/extensions/moo.js:1

```

The file is “general.mjs” and it is located in the same extensions folder as the main code.  
The main code file is found and works.

It contains 1 exported function. It has no syntax errors.  
This is it:

```auto
export function msg(t){
tiled.alert(t);
}

```

What could be going on?

EDIT:

In fact I figured this out.  
In order to use an import it must be named .mjs (as I did). However an .mjs file can only be imported by another .mjs file.  
My main file was .js and that wasn’t going to work. I renamed it .mjs and now my import works.  
Is this a bug or expected?

In the documentation there is no mention of this and the linked javascript module documentation states that you should be able to import a .mjs to a .js file.  
Perhaps the Tiled documentation needs a note about this issue?

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [November 13, 2025, 11:31am UTC](https://discourse.mapeditor.org/t/unexpected-token-import-in-script/7690/2 "2025-11-13T11:31:56Z")

</div>

> [@sancho](#):
>
> Perhaps the Tiled documentation needs a note about this issue?

Hmm, the documentation notes:

> When using the `.mjs` extension, script files are loaded as [JavaScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules). They will then be able to use the [import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) and [export](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export) statements to split up their functionality over multiple JavaScript files. Such extensions also don’t pollute the global scope, avoiding potential name collisions between different extensions.

In wording this, I tried to make it clear that `import` and `export` can only be used _within_ modules, but maybe it should be more explicit?

---

<div class="post-metadata">

**Author:** ![sancho](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@sancho](https://discourse.mapeditor.org/u/sancho)\
**Post date:** [November 15, 2025, 12:59am UTC](https://discourse.mapeditor.org/t/unexpected-token-import-in-script/7690/3 "2025-11-15T00:59:28Z")

</div>

I read that section and didn’t make the connection.  
How about an additional sentence?

`When using the .mjs extension, script files are loaded as `[`JavaScript modules`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)`. They will then be able to use the `[`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import)`and`[`export`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export)`statements to split up their functionality over multiple JavaScript files. Note that import and export are not valid statements when used in files with the .js extension.`
