better-sqlite3 TypeError:o.default 不是构造函数

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

我正在 typescript、webpack 和 better-sqlite3 中创建 vscode 扩展。我尝试在

C:\Users\userName\AppData\Roaming\Code\User\globalStorage\
文件夹中创建数据库。

但我收到错误:

TypeError: o.default is not a constructor

这是我的代码:


import Database from 'better-sqlite3';
import * as fs from "fs";
import { DatabaseNames } from "./database-names.enum";
import { PersistenceRepository } from './persistence.repository';

export class BetterSqliteAdapter implements PersistenceRepository {

    private static instance: BetterSqliteAdapter | null = null;

    db: Database.Database | null = null;

    private constructor() { };


    ;

    public static getInstance() {
        if (!this.instance) {
            this.instance = new BetterSqliteAdapter();
        }
        return this.instance;
    }

    createDatabase(name: DatabaseNames, globalStoragePath: string): void {
        const dbPath = globalStoragePath + "\\" + name + ".db";
        console.log('dbPath :>> ', dbPath);
        if (!fs.existsSync(globalStoragePath)) {
            throw new Error('Extension folder don\'t exist');
        }
        if (!fs.existsSync(dbPath)) {
            try {
                this.db = new Database(dbPath);

            } catch (error) {
                console.log(`Error creating database: ${name}.db ...  error: ${error}`);
            }
        }
    }

这是我的

tsconfig.json
:

{
  "compilerOptions": {
    "module": "CommonJS",
    "target": "ES2020",
    "lib": ["ES2020", "DOM"],
    "sourceMap": true,
    "rootDir": "src",
    "strict": true ,
    "jsx": "react",
    "esModuleInterop": true
  }
}

我也尝试过这个(如何在打字稿中正确并完全导入BetterSqlite3的Database类?)但它不起作用。仍然遇到同样的错误。

更新:

persistence.repository.ts

import { DatabaseNames } from './database-names.enum';

export interface PersistenceRepository {

    /**
     * This method will check if a database with a specific name existe, if not, it will be created.
     * @param name name of the database to check if exist or create.
     */
    createDatabase(name: DatabaseNames, globalStoragePath: string): void;

}

由于它是 vscode 扩展开发,所以我按 F5 启动它。这是

launch.json
(它是来自 yo-code 扩展创建者的默认 launch.json):

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Run Extension",
            "type": "extensionHost",
            "request": "launch",
            "args": [
                "--extensionDevelopmentPath=${workspaceFolder}"
            ],
            "outFiles": [
                "${workspaceFolder}/dist/**/*.js"
            ],
            "preLaunchTask": "${defaultBuildTask}"
        },
        {
            "name": "Extension Tests",
            "type": "extensionHost",
            "request": "launch",
            "args": [
                "--extensionDevelopmentPath=${workspaceFolder}",
                "--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
            ],
            "outFiles": [
                "${workspaceFolder}/out/**/*.js",
                "${workspaceFolder}/dist/**/*.js"
            ],
            "preLaunchTask": "tasks: watch-tests"
        }
    ]
}

这是

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "npm",
            "script": "watch",
            "problemMatcher": "$ts-webpack-watch",
            "isBackground": true,
            "presentation": {
                "reveal": "never",
                "group": "watchers"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "type": "npm",
            "script": "watch-tests",
            "problemMatcher": "$tsc-watch",
            "isBackground": true,
            "presentation": {
                "reveal": "never",
                "group": "watchers"
            },
            "group": "build"
        },
        {
            "label": "tasks: watch-tests",
            "dependsOn": [
                "npm: watch",
                "npm: watch-tests"
            ],
            "problemMatcher": []
        }
    ]
}

这是

package.json
脚本和依赖项:

"scripts": {
    "vscode:prepublish": "npm run package",
    "compile": "webpack --mode development --progress",
    "watch": "webpack --mode development --progress --watch",
    "package": "webpack --mode production ",
    "compile-tests": "tsc -p . --outDir out",
    "watch-tests": "tsc -p . -w --outDir out",
    "pretest": "npm run compile-tests && npm run compile && npm run lint",
    "lint": "eslint src --ext ts",
    "test": "node ./out/test/runTest.js"
  },
  "devDependencies": {
    "@types/better-sqlite3": "^7.6.5",
    "@types/glob": "^8.1.0",
    "@types/mocha": "^10.0.1",
    "@types/node": "20.2.5",
    "@types/react": "^18.2.21",
    "@types/react-dom": "^18.2.7",
    "@types/vscode": "^1.81.0",
    "@typescript-eslint/eslint-plugin": "^5.59.8",
    "@typescript-eslint/parser": "^5.59.8",
    "@vscode/test-electron": "^2.3.2",
    "drizzle-kit": "^0.19.13",
    "eslint": "^8.41.0",
    "glob": "^8.1.0",
    "mini-css-extract-plugin": "^2.7.6",
    "mocha": "^10.2.0",
    "terser-webpack-plugin": "^5.3.9",
    "ts-loader": "^9.4.3",
    "typescript": "^5.1.3",
    "webpack": "^5.85.0",
    "webpack-cli": "^5.1.1"
  },
  "dependencies": {
    "@vscode/webview-ui-toolkit": "^1.2.2",
    "better-sqlite3": "^8.6.0",
    "drizzle-orm": "^0.28.6",
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  }

更新2:

我删除了节点模块,卸载了better-sqlite3并重新安装了它。现在我有以下错误:

TypeError: Cannot read properties of undefined (reading 'indexOf')

如果我删除

try / catch
子句,则不会抛出任何错误。

更新3:

我删除了节点模块,卸载了 better-sqlite3 并再次重新安装。 现在我又收到第一条错误消息了

更新4:

我只是尝试直接使用“fs”创建一个文件来检查我是否具有权限并且我的项目没有配置错误。它运作得很完美。 我更改的代码是:

 createDatabase(name: DatabaseNames, globalStoragePath: string): void {
    ...
    fs.writeFileSync(dbPath, '');
    ...
}
typescript sqlite better-sqlite3
1个回答
0
投票

我通过添加到我的

webpack.config.js
来解决它:

    externals: {
      "better-sqlite3": "commonjs better-sqlite3",
    },
© www.soinside.com 2019 - 2024. All rights reserved.