vite+svelte+pouchdb: Uncaught TypeError: Class extends value [object Object] is not a constructor or null

问题描述 投票:0回答:0

我正在尝试使用 Svelte + PouchDB 设置最低限度的配置,并在浏览器中遇到上述错误。请参阅以下重现问题的步骤。

重现步骤:

  1. 使用 Svelte 模板创建一个基本的 Vite 脚手架项目,并验证它在浏览器中是否按预期工作
$ npm create vite@latest pouchdb-test -- --template svelte
$ cd pouchdb-test
$ npm install
$ npm run dev
  1. 安装PouchDB(或者pouchdb-browser,问题重现一样)

    $ npm install --save pouchdb

  2. 使用 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>

  1. 更新vite.config.js(第7行是我加的,其他的已经有了)
# 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
});

  1. 重建程序并在浏览器中打开应用程序(http://127.0.0.1:5173)。如果您检查控制台,则会抛出此异常:Uncaught TypeError: Class extends value [object Object] is not a constructor or null at index-browser.es.js:457:23

我无法理解为什么会出现此错误以及如何解决。欢迎任何帮助或指点。

svelte vite pouchdb
© www.soinside.com 2019 - 2024. All rights reserved.