As part of my vscode extension development in typescript, webpack, and better-sqlite3, I am attempting to create a database within the
C:\Users\userName\AppData\Roaming\Code\User\globalStorage\
folder.
However, when executing the code, I encounter the following error:
TypeError: o.default is not a constructor
The code snippet causing this error is as follows:
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}`);
}
}
}
This is the tsconfig.json
:
{
"compilerOptions": {
"module": "CommonJS",
"target": "ES2020",
"lib": ["ES2020", "DOM"],
"sourceMap": true,
"rootDir": "src",
"strict": true ,
"jsx": "react",
"esModuleInterop": true
}
}
I have also referred to this link (How to correctly and fully import BetterSqlite3's Database class in typescript?), but unfortunately, it did not resolve the issue.
Update:
The 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;
}
Given that this involves developing a vscode extension, I typically launch it by pressing F5. Here is the launch.json
configuration (it's the default launch.json from yo-code extension creator):
{
"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"
}
]
}
This is the 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": []
}
]
}
Here are the script commands and dependencies from the package.json
:
"scripts": {
...
},
"devDependencies": {
...
},
"dependencies": {
...
}
Update 2:
After some troubleshooting steps, which included deleting node modules, uninstalling and re-installing better-sqlite3, I encountered the following error:
TypeError: Cannot read properties of undefined (reading 'indexOf')
.
Remarkably, removing the try / catch
block prevents any errors from being thrown.
Update 3:
Following another attempt at deleting node modules, uninstalling better-sqlite3, and re-installing it AGAIN, I am back to encountering the initial error message.
Update 4:
To verify permissions and project configuration, I attempted to create a file directly using 'fs'. Surprisingly, it worked without issues. The modified code snippet is:
createDatabase(name: DatabaseNames, globalStoragePath: string): void {
...
fs.writeFileSync(dbPath, '');
...
}