首頁>技術>

今天在Win10上用VSCode執行Vue專案時遇到一個錯誤:vue : 無法載入檔案 D:\Program Files\node-v14.15.1-win-x64\vue.ps1,因為在此係統上禁止執行指令碼。node用的官方綠色版直接配置環境變數,Vue在cmd命令窗全域性口安裝的可以透過vue --servion檢視到Vue版本。

# CMD命令視窗>vue --version@vue/cli 4.5.10# PowerShell> vue --versionvue : 無法載入檔案 D:\Program Files\node-v14.15.1-win-x64\vue.ps1,因為在此係統上禁止執行指令碼。有關詳細資訊,請參閱 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。所在位置 行:1 字元: 1+ vue --version+ ~~~    + CategoryInfo          : SecurityError: (:) [],PSSecurityException    + FullyQualifiedErrorId : UnauthorizedAccess
解決辦法

用管理員的身份執行PowerShell,然後執行命令:get-ExecutionPolicy,如果回覆Restricted表示當前狀態是禁止的 。這時候我們再執行命令:set-ExecutionPolicy RemoteSigned ,在彈出的選項後輸入:y 來更改策略。

> get-ExecutionPolicyRestricted> set-ExecutionPolicy RemoteSigned執行策略更改執行策略可幫助你防止執行不信任的指令碼。更改執行策略可能會產生安全風險,如 https:/go.microsoft.com/fwlink/?LinkID=135170中的 about_Execution_Policies 幫助主題所述。是否要更改執行策略?[Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 暫停(S)  [?] 幫助 (預設值為“N”): y

這樣錯誤提示沒有了,但是接下來又出現錯誤: 'vue-cli-service' 不是內部或外部命令,也不是可執行的程式或批處理檔案。npm WARN Local package.json exists, but node_modules missing, did you mean to install? 這種情況一般是檢測到本地環境未安裝vue-cli,但是很明顯我是全域性安裝了的。

>npm run serve> [email protected] serve C:\Users\fanmi\Desktop\DEMO\ncview> vue-cli-service serve'vue-cli-service' 不是內部或外部命令,也不是可執行的程式或批處理檔案。npm ERR! code ELIFECYCLEnpm ERR! errno 1npm ERR! [email protected] serve: `vue-cli-service serve`npm ERR! Exit status 1npm ERR!npm ERR! Failed at the [email protected] serve script.npm ERR! This is probably not a problem with npm. There is likely additional logging output above.npm WARN Local package.json exists, but node_modules missing, did you mean to install?npm ERR! A complete log of this run can be found in:npm ERR!     C:\Users\fanmi\AppData\Roaming\npm-cache\_logs\2021-01-19T03_37_11_868Z-debug.log
解除安裝重灌

全域性解除安裝Vue我們可以執行命令:npm uninstall -g @vue/cli,解除安裝之後用命令vue -V查下是否還有Vue版本如果出現【'vue' 不是內部或外部命令,也不是可執行的程式或批處理檔案】說明已經解除安裝Vue了。重新安裝新版本npm install -g @vue/cli或透過@指定版本npm install -g @vue/[email protected],安裝之後我們再次透過命令:vue --version檢視Vue版本,如果顯示則安裝成功(這次我用PowerShell安裝)。

> npm uninstall -g @vue/clinpm WARN deprecated [email protected]: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2.removed 1388 packages in 71.758s> vue -V'vue' 不是內部或外部命令,也不是可執行的程式或批處理檔案。> npm install -g @vue/cli>vue --version@vue/cli 4.5.10
重下node_modules

重灌之後還是遇到錯誤: 'vue-cli-service' 不是內部或外部命令,也不是可執行的程式或批處理檔案。刪了node_modules資料夾重新下載依賴,在專案目錄執行命令:npm i ,Vue會根據package.json安裝相關依賴了。在安裝依賴時(npm install)又遇到報錯: npm ERR! cb()never called,管理員模式開啟cmd執行命令:npm cache clean -f清除npm快取 ,然後執行命令:npm install -g n --force安裝npm包管理助手。重新刪除專案中的 package-lock.json 檔案 和 node_modules 資料夾,然後再執行 npm i重新下載node_modules。最後執行npm run serve,啟動成功!

> npm inpm ERR! cb() never called!npm ERR! This is an error with npm itself. Please report this error at:npm ERR!     <https://npm.community>>npm cache clean -fnpm WARN using --force I sure hope you know what you are doing.> npm install -g n --forcenpm WARN using --force I sure hope you know what you are doing.D:\Program Files\node-v14.15.1-win-x64\n -> D:\Program Files\node-v14.15.1-win-x64\node_modules\n\bin\n+ [email protected] 1 package from 4 contributors in 1.064s
總結:

專案是在Ubuntu 18.04系統上建立的,應該是node、npm和vue的版本和專案中對應的版本不匹配造成的吧!以上內容是小編給大家分享的【Vue實戰096:Vue執行無法載入檔案及其他問題解決】。希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。

23
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 力扣刷題指南已開源在Github