Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johnDoe220's avatar

import declarations may only appear at top level of a module

hi and for test export and import in application create two file with name app.js and data.js.

data.js

export const name = 'john';
app.js

import {name} from './data.js'

html code

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	

	<script src="app.js"></script>
</body>
</html>

and give me this error in console

Uncaught SyntaxError: import declarations may only appear at top level of a module
0 likes
5 replies
Sinnbeck's avatar

Did you compile it or just load that app.js file directly?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@johnDoe220 It works for me like that. I created those 2 files in the public directory, and loaded the app.js like I showed above. I added an alert to ensure it worked

import {name} from './data.js'
alert(name)

Please or to participate in this conversation.