vue3.2.X + Vite2.X + Pinia2.X的前端项目常见报错

在vue3.2.X + Vite2.X + Pinia2.X项目中,执行npm install 或者npm run dev 时,报以下几种错误的,均是因为版本不兼容,或者node版本过低导致,具体错误如下:

硬件环境: windows7 64位 + Node12.22.10 

错误一:vue 、vite、node 版本不兼容

error when starting dev server:
Error: Failed to resolve vue/compiler-sfc.
@vitejs/plugin-vue requires vue (>=3.2.25) to be present in the dependency tree.
    at resolveCompiler (E:\2023\intelligent-equipment-screen\node_modules\@vitejs\plugin-vue\dist\index.js:3955:11)
    at Context.buildStart (E:\2023\intelligent-equipment-screen\node_modules\@vitejs\plugin-vue\dist\index.js:4684:46)
    at E:\2023\intelligent-equipment-screen\node_modules\vite\dist\node\chunks\dep-689425f3.js:39232:46
    at Array.map (<anonymous>)
    at Object.buildStart (E:\2023\intelligent-equipment-screen\node_modules\vite\dist\node\chunks\dep-689425f3.js:39230:39)
    at Server.httpServer.listen (E:\2023\intelligent-equipment-screen\node_modules\vite\dist\node\chunks\dep-689425f3.js:60350:37)
    at E:\2023\intelligent-equipment-screen\node_modules\vite\dist\node\chunks\dep-689425f3.js:48219:20
    at new Promise (<anonymous>)
    at httpServerStart (E:\2023\intelligent-equipment-screen\node_modules\vite\dist\node\chunks\dep-689425f3.js:48200:12)
    at startServer (E:\2023\intelligent-equipment-screen\node_modules\vite\dist\node\chunks\dep-689425f3.js:60380:30)

错误二:vue 、vite、node 版本不兼容

node_modules\vite-plugin-vue-setup-extend\node_modules\@vue\compiler-sfc\dist\compiler-sfc.cjs.js:16397
    #fillNegs() {
             ^
SyntaxError: Unexpected token '('
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (E:\2023\intelligent-equipment-screen\node_modules\vite-plugin-vue-setup-extend\dist\index.cjs:3:21)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)

错误三:这个是因为 vue 与 pinia 版本不兼容

error while updating dependencies:
Error: Build failed with 1 error:
node_modules/pinia/dist/pinia.mjs:6:9: ERROR: No matching export in "node_modules/vue-demi/lib/index.mjs" for import "hasInjectionContext"

错误四:npm run install 时报错

file:///F:/test2/node_modules/vite/bin/vite.js:7
    await import('source-map-support').then((r) => r.default.install())
    ^^^^^

SyntaxError: Unexpected reserved word
    at Loader.moduleStrategy (internal/modules/esm/translators.js:140:18)
    at async link (internal/modules/esm/module_job.js:42:21)

错误五:npm run build 时报错

/node_modules/@volar/vue-language-core/out/generators/template.js:202
                        tagOffsetsMap[tag] ??= [];
                                           ^^^

SyntaxError: Unexpected token '??='

node_modules\vue-tsc\out\index.js:75
            return getScript(fileName)?.version ?? '';

错误六:npm run dev/build 时都报错

Error: No known conditions for "./lib/locale/lang/zh-cn" entry in "element-plus" package

看到这个错误,你一定会以为是你的中文包引入错,其实不是,是因为element-plus的版本不一致导致的,我之前用的element-plus版本是3.2.5,然后最近安装的element-plus版本为3.2.7,然后就一直报这个错误,只要将版本降到3.2.5即可。

错误七:eslint src 时报错

> eslint src Oops! Something went wrong! :( ESLint: 8.50.0 E:\2023\projects\working-monitor-web\node_modules\eslint-plugin-vue\lib\rules\no-deprecated-model-definition.js:74     const allowVue3Compat = Boolean(context.options[0]?.allowVue3Compat)                                                        ^ SyntaxError: Unexpected token '.'     at wrapSafe (internal/modules/cjs/loader.js:915:16)     at Module._compile (internal/modules/cjs/loader.js:963:27)     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)     at Module.load (internal/modules/cjs/loader.js:863:32)     at Function.Module._load (internal/modules/cjs/loader.js:708:14)     at Module.require (internal/modules/cjs/loader.js:887:19)     at require (internal/modules/cjs/helpers.js:74:18)     at Object.<anonymous> (E:\2023\projects\working-monitor-web\node_modules\eslint-plugin-vue\lib\index.js:83:39)     at Module._compile (internal/modules/cjs/loader.js:999:30)     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10) npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! vue3.0-ts-vite-framework@0.0.0 lint: `eslint src` npm ERR! Exit status 2 npm ERR! npm ERR! Failed at the vue3.0-ts-vite-framework@0.0.0 lint script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

这个错误是由于eslint-plugin-vue与node版本不兼容导致,目前我通过尝试,eslint-plugin-vue@9.14.1与node@12.22.10是兼容的,所以,node版本为12的,还请注意,eslint-plugin-vue的版本要与之对应。

经过我再三试验,出现以上问题,均是因为依赖的版本不兼容导致,此时可修改依赖的版本,删除package-lock.json,删除node_modules文件夹,修改package.json后,重新执行npm install,即可正常启动项目。

具体要修改的依赖版本内容如下:

{
    "vue": "3.2.36",
    "vite": "2.9.15",
    "pinia": "2.0.33",
    "vue-tsc": "0.35.2",
    "element-plus": "2.3.5",
    "typescript": "4.9.5",
    "eslint-plugin-vue": "9.14.1",
}

以上内容,是本人开发时遇到并且头疼的问题,目前均已解决,如有错误,欢迎在评论区指出!

  • 38
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
你好!对于使用 Vue 3、TypeScript 和 Vite 来搭建项目,并结合 Pinia 进行状态管理,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了 Node.js 和 npm(或者使用 yarn)。 2. 创建一个新的项目文件夹,并进入该文件夹。 3. 在终端中运行以下命令来初始化一个新的 Vue 3 项目: ``` npm init vite@latest ``` 在初始化过程中,选择使用 Vue 3、TypeScript 和默认配置。 4. 进入项目文件夹并安装依赖: ``` cd <project-folder> npm install ``` 5. 接下来,安装 Pinia: ``` npm install pinia ``` 6. 在 `src` 目录下创建一个 `store` 文件夹,并在其中创建名为 `index.ts` 的文件。 7. 在 `index.ts` 中编写你的 Pinia store。例如,你可以创建一个名为 `counter` 的 store,并且在其中定义一个状态和一些操作: ```typescript import { defineStore } from 'pinia'; export const useCounterStore = defineStore('counter', { state: () => ({ count: 0, }), actions: { increment() { this.count++; }, decrement() { this.count--; }, }, }); ``` 8. 在应用的入口文件 `main.ts` 中导入并使用 Pinia: ```typescript import { createApp } from 'vue'; import { createPinia } from 'pinia'; import App from './App.vue'; const app = createApp(App); const pinia = createPinia(); app.use(pinia); app.mount('#app'); ``` 9. 在组件中使用 Pinia store。在你的 Vue 组件中,可以使用 `useStore` 函数来访问 Pinia store: ```typescript import { useCounterStore } from '../store'; export default { setup() { const counterStore = useCounterStore(); return { counterStore, }; }, }; ``` 10. 最后,你可以在组件中使用 `counterStore` 来访问状态和操作: ```vue <template> <div> <p>{{ counterStore.count }}</p> <button @click="counterStore.increment()">Increment</button> <button @click="counterStore.decrement()">Decrement</button> </div> </template> <script> import { useCounterStore } from '../store'; export default { setup() { const counterStore = useCounterStore(); return { counterStore, }; }, }; </script> ``` 这样,你就可以使用 Vue 3、TypeScript、Vite 和 Pinia 搭建一个基本的项目并进行状态管理了。希望对你有帮助!如果还有其他问题,请随时问我。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值