Har*_*nan 7 pouchdb svelte vite
我正在尝试使用 Svelte + PouchDB 设置最低配置,并在浏览器中遇到上述错误。请参阅下面的步骤来重现该问题。
重现步骤:
$ npm create vite@latest pouchdb-test -- --template svelte
$ cd pouchdb-test
$ npm install
$ npm run dev
Run Code Online (Sandbox Code Playgroud)
安装PouchDB(或pouchdb-browser,问题同样重现)
$ npm install --save pouchdb
使用 PouchDB 创建数据库(下面的第 2 行和第 3 行是我添加的。其他行按原样存在。)
# src/lib/Counter.svelte
<script>
import PouchDB from "pouchdb"; // <--- or "pouchdb-browser" if that is installed
const db = new PouchDB('myDB'); // <--- Add this line too
let count = 0
const increment = () => {
count += 1
}
</script>
<button on:click={increment}>
count is {count}
</button>
Run Code Online (Sandbox Code Playgroud)
# vite.config.js
import {defineConfig} from 'vite';
import {svelte} from '@sveltejs/vite-plugin-svelte';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [svelte()],
define: {global: {}} // <--- Add this line. If not present then this exception occurs:
// Uncaught ReferenceError: global is not defined
// at node_modules/immediate/lib/mutation.js (mutation.js:6:16)
// at __require (chunk-DFKQJ226.js?v=c3a35530:8:50)
// at node_modules/immediate/lib/index.js (index.js:5:3)
// at __require (chunk-DFKQJ226.js?v=c3a35530:8:50)
// at index-browser.es.js:1:23
});
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么会出现此错误以及如何解决它。非常欢迎任何帮助或指示。
Har*_*nan 16
回答我自己的问题。2 个更改有助于解决该问题。
它是从 PouchDB 存储库中报告的类似问题中发现的https://github.com/pouchdb/pouchdb/issues/8607(因此积分归该问题的作者所有)
以下是变化:
export default defineConfig({
plugins: [svelte()],
define: {global: "window"} // <--- Add "window" here
});
Run Code Online (Sandbox Code Playgroud)
npm install events| 归档时间: |
|
| 查看次数: |
752 次 |
| 最近记录: |