使用unplugin-vue-component的Vue项目,组件引入报错:Module not found: Error: [CaseSensitivePathsPlugin] xxx的解决方案

最近,在使用VScode开发Vue项目时,遇到引入组件首字母大小写修改导致terminal报错的问题,错误如下所示:

Module not found: Error: [CaseSensitivePathsPlugin] `/Users/xxx/src/components/ListItem/richList.vue` does not match the corresponding path on disk `RichList.vue`.

在项目启动状态,以新建组件RichList.vue为例。

假设我们的原计划是创建组件RichList.vue,但是不小心创建为richList.vue,在页面中引richList组件,如下:

<template>
  <div><rich-list /></div>
</template>

<script setup></script>

<style lang="scss" scoped></style>

在components.d.ts会自动生成一条引入,如下:

import '@vue/runtime-core'
export {}

declare module '@vue/runtime-core' {
  export interface GlobalComponents { 
    RichList: typeof import('./src/components/ListItem/richList.vue')['default']
  }
}

此时,我们发现程序正常运行,当发现组件名称不符合命名规范,将组件名字改为RichList.vue后,在terminal中看到下图中的报错,提示引入组件的路径不对。 

此时再次打开components.d.ts文件,发现RichList组件的import的路径并没有改变,依旧是首字母小写时候的路径,尝试直接删除components.d.ts引用,或者将./src/components/ListItem/richList.vue修改为./src/components/ListItem/RichList.vue,发现自动引入中的路径更新后还是会生成./src/components/ListItem/richList.vue,terminal依旧报错,经过试验,总结了两种解决上述报错的方案。

方法一:

组件名称增加新的单词变成新的组件名,此方法不仅是新建的组件适用,已经提交到git中的组件同样适用。

例如新组建名称TestRichList,引入组件:

<template>
  <div><test-rich-list /></div>
</template>

<script setup></script>

<style lang="scss" scoped></style>

components.d.ts将重新生成正确的引入,报错消失。

import '@vue/runtime-core'
export {}

declare module '@vue/runtime-core' {
  export interface GlobalComponents { 
    TRichList: typeof import('./src/components/ListItem/TestRichList.vue')['default']
  }
}

方法二:

不重新起名,仅修改大小写。此方法适用从未提交到git上的组件,已提交到git上的组件将不会生效(采用方法一)。

组件名字从richList.vue改为RichList.vue,重启服务,报错消失,且components.d.ts更新成正确的引用。

import '@vue/runtime-core'
export {}

declare module '@vue/runtime-core' {
  export interface GlobalComponents { 
    RichList: typeof import('./src/components/ListItem/RichList.vue')['default']
  }
}

### Webpack 5 中 Axios 和 HTTP 模块未找到错误的解决方案 在 Webpack 5 的环境中,如果遇到 `axios` 或者 `http` 模块无法被识别的情况,通常是因为 Webpack 5 默认不再将 Node.js 核心模块视为内置支持[^1]。这意味着像 `fs`, `path`, `http` 这样的核心模块不会自动加载到打包后的文件中。 #### 解决方案一:配置 Polyfill 来兼容 Node.js 核心模块 为了使这些模块能够正常工作,可以通过安装并配置 `node-polyfill-webpack-plugin` 插件来实现兼容性处理: ```javascript // 安装插件 npm install --save-dev node-polyfill-webpack-plugin ``` 随后,在 Webpack 配置文件中引入该插件并设置如下: ```javascript const NodePolyfillPlugin = require('node-polyfill-webpack-plugin'); module.exports = { resolve: { fallback: { "http": false, // 如果不需要 polyfill 可以设为 false "https": false, "stream": require.resolve("stream-browserify"), "crypto": require.resolve("crypto-browserify") } }, plugins: [ new NodePolyfillPlugin() ] }; ``` 上述代码通过定义 `resolve.fallback` 属性指定了哪些缺失的核心模块应该由特定包替代。对于某些不需浏览器端模拟的功能可以直接禁用(如上例中的 `http` 和 `https` 设置为 `false`)。 #### 解决方案二:手动调整依赖版本 有时问题可能源于所使用的库版本过高或过低而与当前环境不适配。例如,确保已正确安装最新版或者指定版本的 `axios`: ```bash npm install axios@latest ``` 另外需要注意的是,当项目中有多个地方引用不同版本的相同库时也可能引发冲突,因此建议统一管理依赖树结构,可以借助工具命令清理冗余项: ```bash npx npm-check-updates -u npm install ``` 以上方法可以帮助修复因缺少必要模块而导致的应用运行失败情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值