Ask coding questions

← Back to all posts
SyntaxError: import declarations may only appear at top level of a module
[deleted]

I'm making a js library like p5.js when I run the repl it says
SyntaxError: import declarations may only appear at top level of a module

Answered by firefish (953) [earned 5 cycles]
View Answer
Comments
hotnewtop
firefish (953)

There are a couple of problems here:

<script src="/script.js"></script>

is incorrect. You cannot use the import statement outside of a module. You need to do this instead:

<script src="/script.js" type="module"></script>

However, the import statement must be used like this:

import { setup } from './js_tool.js';

Note: the braces here, and the ./ (this means that it is in the current directory, which is needed otherwise js will complain)

dolphin30 (0)

wow, add me to it

[deleted]

It works, thanks