日本語

Fixing “Unexpected token ‘export'” in JavaScript

​​JavaScript​Chromium,ES Modules,esbuild,Troubleshooting

When you load a JavaScript file that contains export with <script src="…">, Chromium logs Uncaught SyntaxError: Unexpected token 'export' to the console. A plain <script> tag reads the file as a classic script, not as an ES module. If you add type="module" to the <script> that uses the code and load the file with import, the error goes away.

This error appears when you add a file from an npm library, or a function copied from another page, to an HTML page with <script src>. I reproduced the error on purpose in a test environment, and confirmed the message and that the fix removes it.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Chromium 153.0.8010.12 (headless) launched with Playwright 1.63.0, and esbuild 0.28.2. The page was served from http://127.0.0.1:8080/ by a small Node.js server that returns .js files as text/javascript. I got the messages through the Chrome DevTools Protocol (CDP) and the page's error event. I did not check how DevTools displays them, and I did not test messages in Firefox or Safari.

Reading a file with export through a plain script tag

I prepared the following greet.js. It exports a function that takes a name and returns a greeting.

js
export function greet(name) {
  return `こんにちは、${name}さん`;
}

I loaded it with the following broken.html (I omitted the <head> and the <style> used for appearance).

html
<p id="out">(まだ何も表示していません)</p>
<script src="greet.js"></script>
<script>
  document.getElementById('out').textContent = greet('ねこ');
</script>

Opening the page produced two errors in a row.

text
Uncaught SyntaxError: Unexpected token 'export'
Uncaught ReferenceError: greet is not defined

The first error happens at line 1, column 1 of greet.js, which is where export is. It means that export cannot be written at that position. Because this is a syntax error, none of greet.js runs, so the greet function is never created. That is why the next <script>, which calls greet('ねこ'), also produces the second error, greet is not defined.

A page showing only a Japanese placeholder that means "Nothing has been displayed yet" inside a frame
The page with greet.js read through a plain script tag. The greeting is not displayed

Changing how export is written did not change the message. export default function …, export const b = 2;, and the form that declares a constant on line 1 and then exposes it on line 2 with export { a }; all produced Uncaught SyntaxError: Unexpected token 'export'. In the last case, the error position is column 1 of line 2, where export is.

The JavaScript modules guide on MDN says that import and export can only be used inside modules. The browser reads a file as a module when the <script> has type="module" (MDN: the script element's type attribute).

Adding type="module" and importing from the user side

Here is the fixed fixed.html. I removed the <script src> that loaded greet.js, added type="module" to the <script> that uses the code, and used import inside it.

html
<p id="out">(まだ何も表示していません)</p>
<script type="module">
  import { greet } from './greet.js';
  document.getElementById('out').textContent = greet('ねこ');
</script>

When I opened this page, no errors appeared, and the screen showed the greeting "Hello, Neko-san" (the text on screen is in Japanese). I did not change greet.js.

A page with a frame showing the greeting "Hello, Neko-san" (the label is in Japanese)
The page with the code loaded through import in a script with type="module". The greeting is displayed

Adding type="module" only to the script tag that reads greet.js does not fix it

I also tried a page where only the first line was changed to <script type="module" src="greet.js"></script>, and the <script> that uses the code was left as it was. The Unexpected token 'export' error disappeared, but Uncaught ReferenceError: greet is not defined remained, and the text on the screen did not change.

According to the MDN guide, functions and variables declared inside a module are not visible from outside it. Only a module that imports them can use the exported items. Unlike a plain <script>, this does not create a global greet. Also, as the MDN script element page explains, module scripts are deferred automatically (they behave like defer). So even if they come earlier in the HTML, they do not run before a plain <script> placed after them.

Writing import in a plain script tag causes a different error

If you write import { greet } from './greet.js'; in a <script> without type="module", the message changes to the following.

text
Uncaught SyntaxError: Cannot use import statement outside a module

This means that an import statement cannot be used outside a module. The cause is the same as with Unexpected token 'export'. In this case too, adding type="module" to the <script> fixes it.

If you have to read the file from a <script> that cannot be changed to type="module" because of existing code, you can call import() as a function. The MDN page for import() also describes it as a way to load a module from a non-module context. With the following code (I omitted <p id="out"> and similar elements), no error appeared, and the greeting "Hello, Neko-san" was displayed.

html
<script>
  import('./greet.js').then(({ greet }) => {
    document.getElementById('out').textContent = greet('ねこ');
  });
</script>

import() runs the code in then after the file has been loaded. Until loading finishes, greet cannot be used.

Opening the HTML file directly causes a CORS error instead

When I opened the fixed fixed.html at a file:// URL without a server, the greeting was not displayed, and the console showed the following two lines (I replaced the file location with /path/to/app).

text
Access to script at 'file:///path/to/app/greet.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: chrome, chrome-untrusted, data, http, https.
Failed to load resource: net::ERR_FAILED

The MDN export page also says that modules do not work with file:// URLs and must be served from an HTTP server. When you test locally, start some kind of development server and open the page at an http:// URL. The server also has to return .js files with a JavaScript MIME type such as text/javascript (from the MDN guide).

When you cannot use type="module", convert to IIFE format

On an older page, you may want to call the code as a global function from other plain <script> tags. In that case, you can use a bundler to convert the file, which contains export, into a form that works in a plain <script>. An IIFE (Immediately Invoked Function Expression) is a function that is defined and called right away. With esbuild, passing --format=iife wraps the whole file contents in this kind of function, and --global-name sets the name of the global variable that holds the exported items.

I installed esbuild in the project with npm i -D esbuild@0.28.2 and then used it.

bash
npx esbuild greet.js --bundle --format=iife --global-name=Neko --outfile=greet.iife.js

The resulting greet.iife.js has the form var Neko = (() => { … })();, and export is gone. Loaded through a plain <script>, it can be called as Neko.greet.

I checked this on the following page (I omitted <p id="out"> and similar elements).

html
<script src="greet.iife.js"></script>
<script>
  document.getElementById('out').textContent = Neko.greet('ねこ');
</script>

This page also showed no errors, and the greeting "Hello, Neko-san" was displayed.

​Posted by 86