admin管理员组文章数量:1401596
I am trying to make simple example of vanilla ES import export.
index.js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script type="module" src="main.js"></script>
</head>
<body>
</body>
</html>
main.js
import {foo} from './mathModule';
console.log(foo);
mathModule.js
export const foo = Math.sqrt(2);
when I run this page I get an error
main.js:1 GET http://[page]/test/mathModule 404 (Not Found)
EDIT: project structure
- test
- index.html
- main.js
- mathModule.js
I am trying to make simple example of vanilla ES import export.
index.js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script type="module" src="main.js"></script>
</head>
<body>
</body>
</html>
main.js
import {foo} from './mathModule';
console.log(foo);
mathModule.js
export const foo = Math.sqrt(2);
when I run this page I get an error
main.js:1 GET http://[page]/test/mathModule 404 (Not Found)
EDIT: project structure
- test
- index.html
- main.js
- mathModule.js
- 1 Does that URL exist? – SLaks Commented Jul 17, 2018 at 17:33
- @SLaks it should be mathModule.js – tprieboj Commented Jul 17, 2018 at 17:35
- Then change your import to match that. – SLaks Commented Jul 17, 2018 at 17:36
-
2
Why add
<script>
for both if main.js imports the dependency itself? Also you must add the ".js" extension in theimport
statement. – zero298 Commented Jul 17, 2018 at 17:37 - 1 In which browser are you running this? (Downvote wasn't from me. I just upvoted) – Nimeshka Srimal Commented Jul 17, 2018 at 17:42
2 Answers
Reset to default 7import
needs a fully qualified URL. You can't leave off the extension unless the absolute URL doesn't have an extension on it.
So judging by your examples use:
import {foo} from './mathModule.js';
As Nimeshka Srimal caught, it looks like the extension requirement varies between implementations. Firefox is appending .js
automatically, but Chrome and Safari expect the actual address.
I'm looking at the spec, 15.2.2 Imports, and there doesn't seem to be any specification on whether the implementer should append the extension automatically or not.
Additionally, as ASDFGerte pointed out from the MDN docs on import
:
The module to import from. This is often a relative or absolute path name to the .js file containing the module. Certain bundlers may permit or require the use of the extension; check your environment. Only single quotes and double quotes Strings are allowed.
The simplest would be to remove the export
and import
expressions since you're already including both files in your html.
const foo = Math.sqrt(2) // mathModule.js
console.log(foo) // main.js
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<script src="mathmodule.js"></script>
<script src="main.js"></script>
</head>
<body>
</body>
</html>
If you are using node or something similar to run the script, maybe you should use a transpiler such as babel. Import
and Export
are "new" javascript features hence they're not implemented in most browsers
本文标签: javascriptES module import not workingStack Overflow
版权声明:本文标题:javascript - ES module import not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744225644a2596074.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论