vite3安裝eslint報(bào)錯(cuò)

在初始化vite3 + vue3項(xiàng)目安裝eslint時(shí)報(bào)錯(cuò)如下:

[vite] Internal server error: Cannot read config file: D:\vue3-template\.eslintrc.js
Error: require() of ES Module D:\vue3-template\.eslintrc.js from D:\vue3-template\node_modules\@eslint\eslintrc\dist\eslintrc.cjs not supported.
.eslintrc.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename .eslintrc.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in D:\vue3-template\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

在分析官方提示后發(fā)現(xiàn)應(yīng)該在package.json文件中修改type為commonjs或?qū)?eslintrc.js文件名改為.eslintrc.cjs便可解決該問題

原因分析:

  1. type字段的產(chǎn)生用于定義package.json文件和該文件所在目錄根目錄中.js文件和無拓展名文件的處理方式。值為'moduel'則當(dāng)作es模塊處理;值為'commonjs'則被當(dāng)作commonJs模塊處理
  2. 目前node默認(rèn)的是如果pacakage.json沒有定義type字段,則按照commonJs規(guī)范處理
  3. node官方建議包的開發(fā)者明確指定package.jsontype字段的值
  4. 無論package.json中的type字段為何值,.mjs的文件都按照es模塊來處理,.cjs的文件都按照commonJs模塊來處理
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容