You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ES6脚本中使用require()引入CSS文件出现解析错误求助

Fixing CSS Parsing Errors When Using require() in ES6 <script> Tags

Hey there, let's tackle those CSS parsing errors you're seeing when using require() in your ES6 <script> tag! Here's what's going on and how to fix it:

Why This Happens

require() is a CommonJS syntax, while you're working in an ES6 module environment (since you're already using import). Most modern build tools (like Webpack, Vite, or Vue CLI) support mixing CommonJS and ES modules to some extent, but they often handle non-JavaScript resources like CSS better when using standard ES6 import syntax. When you use require() for CSS, your build tool might not trigger the correct loader to process and inject the styles, leading to parsing errors.

The Quick Fix: Switch to import for CSS Files

The simplest solution is to replace your require() calls with ES6 import statements for the CSS files. This aligns with your existing module syntax and ensures your build tool processes the styles correctly:

<script>
import CalendarView from "vue-simple-calendar"
import CalendarMathMixin from "vue-simple-calendar/dist/calendar-math-mixin.js"
import "vue-simple-calendar/dist/static/css/default.css"
import "vue-simple-calendar/dist/static/css/holidays-us.css"
// Rest of your code...
</script>

Additional Checks If the Issue Persists

If you still run into problems after switching to import, here are a few things to verify:

  • Build Tool Configuration: Ensure your build tool has the necessary loaders set up for CSS. For Webpack, this means having css-loader and style-loader (or mini-css-extract-plugin) in your webpack.config.js. Vite and Vue CLI include these configurations by default, so you shouldn't need to add them manually.
  • Browser Native ES Modules: If you're using native ES modules without a build tool (directly in the browser), require() isn't supported at all—so import is your only option here. Modern browsers support importing CSS files directly via import statements.

That should resolve the parsing errors and get your calendar styles loading correctly!

内容的提问来源于stack exchange,提问作者Oliver Williams

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:50:59