admin管理员组文章数量:1404772
I receive an error whenever I import a library in my index.js
file and try to use it in index.html
file.
script tag in index.html
:
<script src="index.js" type="module"></script>
import statement in index.js
: import axios from './node_modules/axios';
The error I receive:
*I am running the app on a local server, not on the file system.
I receive an error whenever I import a library in my index.js
file and try to use it in index.html
file.
script tag in index.html
:
<script src="index.js" type="module"></script>
import statement in index.js
: import axios from './node_modules/axios';
The error I receive:
*I am running the app on a local server, not on the file system.
Share Improve this question asked Feb 5, 2020 at 16:34 DavidDavid 1792 silver badges15 bronze badges 7-
You don't need to give path for files
node_modules
i think – Maheer Ali Commented Feb 5, 2020 at 16:35 - It doesn't work otherwise. – David Commented Feb 5, 2020 at 16:36
- setting up you localhost server to set a correct MIME type ? – Pierre Commented Feb 5, 2020 at 16:36
- How do I do that? – David Commented Feb 5, 2020 at 16:37
- It seems your server is returning a MIME type of text/html. Possibly check your server to make sure it is accessing the correct return/endpoint? – omoshiroiii Commented Feb 5, 2020 at 16:37
3 Answers
Reset to default 3if you use just vanilla JavaScript and you want to use module you have to add in script tag type module for index.js by this way you tell browser you have to support module in index.js, and after you use module normally with exception you have to add extension name like ".js" for each import Good Luck
<script type="module" src="main.js"></script>
You need to provide the URL to an ES6 module file, not the URL to an automatically generated HTML document showing an index of files in the axios
directory.
The Axios distribution includes:
node_modules/axios/dist/axios.min.js
… but that appears to be a hybrid "Load with a regular script tag in the browser" and CommonJS module — not an ES6 module, so you can't import
it.
Consider using a tool like Webpack instead.
You can use CDN instead of that.
<script src="https://cdnjs.cloudflare./ajax/libs/axios/0.19.2/axios.min.js"></script>
Thank you
本文标签: How to load a javascript module in htmlStack Overflow
版权声明:本文标题:How to load a javascript module in html - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744842855a2628027.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论