我正在尝试使用 Svelte + PouchDB 设置最低限度的配置,并在浏览器中遇到上述错误。请参阅以下重现问题的步骤。
重现步骤:
$ npm create vite@latest pouchdb-test -- --template svelte
$ cd pouchdb-test
$ npm install
$ npm run dev
安装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>
# 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
});
我无法理解为什么会出现此错误以及如何解决。欢迎任何帮助或指点。