Skip to content
This repository was archived by the owner on Jul 1, 2026. It is now read-only.
This repository was archived by the owner on Jul 1, 2026. It is now read-only.

When building a vscode extension using sqlite3, database creation fails #1806

Open

Description

@gord47

Issue Summary

This vscode extension is meant to create or read from an sqlite database on activation using the node-sqlite3 module. However, when debugging, the following error appears:
Activating extension failed: Could not find module root given file: "node:internal/modules/cjs/loader". Do you have a package.json file? .
This is despite installing sqlite3 and @types/sqlite3 prior to debugging. The way to database is created is as follows:

import * as vscode from 'vscode';
import Database from 'sqlite3';
import * as fs from 'fs';
import * as path from 'path';
import { registerCommands } from './commandRegistration';
import { TreeDataProvider } from './treeDataProvider';
import { YourViewProvider } from './yourViewProvider';

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export const db = new Database('localcsr.db');
export const treeDataProvider = new TreeDataProvider();
export function activate(context: vscode.ExtensionContext) {
    ....
}
export function deactivate() {}

Steps to Reproduce

I expected a database to be created within folder the extension was activated in. The steps to reproduce are highlighted above.

Version

^5.1.7

Node.js Version

20.18.0

How did you install the library?

npm install sqlite3 then perform .\node_modules.bin\electron-rebuild.cmd -v 32.2.1

Activity

  1. jack484 commented on Nov 29, 2024

    @jack484

    I have also encountered this problem

  2. smith-xyz commented on Oct 9, 2025

    @smith-xyz

    Just to double check:

    import Database from 'sqlite3';

    I don't think that is a default export from sqlite3, should be:

    import { Database } from 'sqlite3';

    I created a little extension setup and didn't seem to have an issue:

    import * as vscode from "vscode";
    import { Database } from "sqlite3";
    import * as path from "path";
    
    let db: Database | undefined;
    
    export function activate(context: vscode.ExtensionContext) {
      console.log("SQLite3 Extension is now active!");
    
      try {
        const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
        const dbPath = workspaceFolder
          ? path.join(workspaceFolder.uri.fsPath, "extension.db")
          : path.join(context.extensionUri.fsPath, "extension.db");
    
        console.log("Database path:", dbPath);
    
        db = new Database(dbPath, (err) => {
          if (err) {
            console.error("Database initialization failed:", err.message);
            vscode.window.showErrorMessage(`SQLite3 Error: ${err.message}`);
          } else {
            console.log("Database initialized successfully");
            vscode.window.showInformationMessage("SQLite3 database ready!");
          }
        });
      } catch (error: any) {
        console.error("Fatal database error:", error.message);
        vscode.window.showErrorMessage(`Fatal SQLite3 Error: ${error.message}`);
      }
    
      const testCommand = vscode.commands.registerCommand(
        "sqlite3-extension.test",
        () => {
          if (!db) {
            vscode.window.showErrorMessage("Database not initialized");
            return;
          }
    
          const testDb = new Database(":memory:", (err) => {
            if (err) {
              vscode.window.showErrorMessage(`Test failed: ${err.message}`);
            } else {
              vscode.window.showInformationMessage("SQLite3 test passed!");
              testDb.close();
            }
          });
        }
      );
    
      context.subscriptions.push(testCommand);
    }
    
    export function deactivate() {
      if (db) {
        db.close();
      }
    }

    Environment Details:
    OS:

    macOS 15.6.1 (Build 24G90)
    Architecture: x86_64 (Intel)

    VS Code:

    Version: 1.104.2
    Architecture: x64

    Node.js & Runtime:

    Node.js: v20.18.0
    npm: 11.0.0
    Electron Modules Version: 115
    V8: 11.3.244.8-node.23

    SQLite3 Package:

    sqlite3: 5.1.7
    electron-rebuild: 3.2.9
    Electron target version: 32.2.1 (as configured in package.json)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions