2025-12-18

Tauri Plugin Bug: Uncaught TypeError: Failed to resolve module specifier

這是我在開發 Tauri APP 時遇到的問題。以下是我的錯誤訊息:

Uncaught TypeError: Failed to resolve module specifier "@tauri-apps/plugin-store". Relative references must start with either "/", "./", or "../".

原始碼

我的程式碼如下

index.html
<!doctype html>
<html lang="zh-Hant-TW">
<head>
<script type="module" src="./store.js" defer></script>
</head>
<body>
<!-- 中略 -->
</body>
</html>
javascript (store.js)
import { load } from '@tauri-apps/plugin-store'
const store = await load('data-store.json', { autoSave: false })
store.get('foo')

狀況

我的程式碼會在 APP 剛啟動就讀取 store 資料

但不管執行幾次、不管如何改,都沒法讀取

解決辦法

後來我開 Web View 的開發人員工具,看到 store.js 的程式根本沒正常執行,主控台則是回報了開頭說的錯誤

經查詢,只要把引用 plugin 改成這種寫法即可:

const { load } = window.__TAURI__.store

即:

const { load } = window.__TAURI__.store

const store = await load('data-store.json', { autoSave: false })
store.get('foo')

參考資料

Shawon. (2023, November 22). Re: (Tauri) js import not working: “Failed to resolve module specifier.” Stack Overflow. https://stackoverflow.com/a/77531104

沒有留言:

張貼留言