简体   繁体   English

using.mjs 文件扩展名 js 模块

[英]using .mjs file extension js modules

I have a problem concerning.js and.mjs file extensions.我有一个关于 .js 和 .mjs 文件扩展名的问题。 When I use <script type = "module" src="test.js"></script> as a html script import statement everything works fine.当我使用<script type = "module" src="test.js"></script>作为 html 脚本导入语句时,一切正常。

But if I use <script type = "module" src="test.mjs"></script> as import statement, I get the following error message:但是,如果我使用<script type = "module" src="test.mjs"></script>作为导入语句,我会收到以下错误消息:

"Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec." “加载模块脚本失败:服务器以“”的非 JavaScript MIME 类型响应。按照 HTML 规范对模块脚本强制执行严格的 MIME 类型检查。

I use a local XAMPP Apache Webserver for testing purposes.我使用本地 XAMPP Apache 网络服务器进行测试。 Does somebody know, how I can tell my browser that.mjs is a javascript file?有人知道,我怎么能告诉我的浏览器 that.mjs 是 javascript 文件?

Thank you for your help!谢谢您的帮助!

If the server does not respond with the correct MIME-Type for .mjs or other files, you may be able to add this Type in your .htaccess with如果服务器没有响应.mjs或其他文件的正确 MIME 类型,您可以在.htaccess中添加此类型

<IfModule mod_mime.c>
  AddType text/javascript mjs
</IfModule>

That solved the problem for me.这为我解决了问题。

But I also decided to use the .js extension again for my modules, because I think the time for .mjs as extension has not come yet.但我也决定再次为我的模块使用.js扩展名,因为我认为.mjs作为扩展名的时间还没有到来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个使用原生 JS 模块在浏览器和 NodeJS 上运行的 .mjs 文件 - One .mjs file that works on the browser and NodeJS using native JS Modules 在客户端导入 JavaScript 模块:.js 或 mjs - Import JavaScript modules on client side : .js or mjs 目标“main”中的意外输出文件类型为 .html(^^^^^^^^^^^^ 文件扩展名必须为 .js、.mjs 或 .cjs) - Unexpected output file type .html in target "main" (^^^^^^^^^^^^ File extension must be .js, .mjs, or .cjs) 为什么 Javascript 模块在导入时找不到带有“.js”文件扩展名的模块,除非文件扩展名是 .mjs,尽管 package“类型”是“模块”? - Why is Javascript module with ".js" file extension not found on import unless file extension is .mjs despite package "type" being "module"? 使用 Node 网络服务器导入 .js 或 .mjs 文件 - Importing .js or .mjs files using Node webserver 如何在 Jest 的 xyz.test.js 中导入“.mjs”模块? - How to import ".mjs" modules in Jest's xyz.test.js? 哪个应该有.mjs 扩展名,导入文件或导出文件,或两者兼而有之? - Which should have .mjs extension, the importing file or the exporting file, or both? Nodejs - 将 mjs 文件中的内容导入 js 文件或如何在 mjs 文件中使用 require? - Nodejs - import stuff from mjs file into js file OR how to use require in mjs file? JavaScript ES6 模块基本问题:.mjs 文件扩展名和 MIME 类型 - JavaScript ES6 Module basic question: .mjs file extension and MIME type 是否可以使用Chrome扩展程序在浏览器上运行Node.js模块 - Is it possible to run Node.js modules on browser using Chrome extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM