JS Module Import from Module Results in 404
Answered
Dear all
I have a rather simple project with one html file and a couple of JS files. The html file imports the main JS file using a type="module" script tag. This file then imports functions from other JS files using the JS import syntax using relative (absolute paths produce no different result) paths. This doesn't happen when I open files using a “normal” webserver in e.g. VSCode.
Steps to reproduce:
- Open the project folder in IntelliJ IDEA.
- Open the index.html file in the editor
- Open the file in the browser by (both produce the same result)
- clicking the run icon in the current file
- clicking on the browser icon in the corner of the editor (popup in the corner of the editor window)
- Open the developer tools in the browser.
- Switch to the Console view.
Here's my MWE (https://we.tl/t-8Yx3hD3CoN). I couldn't find a way to upload a file here.
What's going wrong here? Is this a user error on my end? In that case I would say that the user flow is not clear. Or does IntelliJ simply not support this functionality?
Cheers
Please sign in to leave a comment.
Hello!
in your
index.html, please remove the leading slash from thesrcURL, like:<script src="assets/js/app.js" type="module"></script>IDEA simple web server serves files from
<root>/<project name>, likehttp://localhost:63342/todo/assets/js/fullscreen.js; any absolute URL (starting with leading slash, likesrc="/assets/js/app.js) that tell the browser to resolve a link relative to web server root will result in 404 error, breaking the files loading.Hello Elena
Thank you for the help. That makes sense. I hadn't noticed the server starting from the parent directory. This should mean that the script app.js shouldn't load at all, then. When I don't change the path in the script Tag, so I'm still using the absolute path, the script still executes, though. In this MWE, I would expect to get a 404, becuase I'm trying to load a script that doesn't exist at that path. Instead the exception I throw in the app.js Script is executed. Something doesn't add up for me here.
Cheers
Tyler
Due to some fixes we added in scope of https://youtrack.jetbrains.com/issue/WEB-11949, most of absolute paths work on the built-in web server, see https://youtrack.jetbrains.com/issue/WEB-11949/simplify-built-in-web-server-document-root-configuration#focus=Comments-27-5029164.0-0. But some problems still remain, see https://youtrack.jetbrains.com/issue/WEB-57280/Built-in-web-server-URL-links-in-CSS-dont-work-if-CSS-is-loaded-using-absolute-path-with-leading-slash, for example. So the first script is loaded, but the URLs in it are not correctly resolved.
Tha's why I strongly recommend changing all paths to relative ones as I suggested in my previous post.
I see. Makes sense. Thank you!