1

i am facing a problem with imports in js for about 6 days now. I just can't solve that problem. I have a simple flask app written in python and a little bit of js code running on the client. I have two js files and i want to import a variable from the first in the second script. I don't know whats the problem, i tried it with several combinations but it just wont run.

first.js:

// Local Config
let ADD_SONG_URL = "TEST";
export {ADD_SONG_URL};

second.js:

import {ADD_SONG_URL} from "./first.js";
console.log(ADD_SONG_URL);

file.html:

<script type="module" src="/static/js/first.js"></script>
<script type="text/javascript" src="/static/js/second.js"></script>

In Intellij everything looks fine, but in the browser i am getting Uncaught SyntaxError: Cannot use import statement outside a module. I am relatively new to frontend programming so i'm desperate because i can't fix this problem. I hope anyone have a quick answer and thats not a big thing.

Thanks in advance!

1 Answer 1

8

Try changing your script type from text/javascript to module. I think it should work.

<script type="module" src="/static/js/first.js"></script>
<script type="module" src="/static/js/second.js"></script>
Sign up to request clarification or add additional context in comments.

2 Comments

Okay wow! :D thats just working. Could you explain why its so important that both are type=module? i thought the main script should still have the text/javascript type cause literally it is the script.
Your script type have to be module if you are using import and export in them. You can have a look javascript.info/modules-intro

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.