WiseLibs / better-sqlite3

The fastest and simplest library for SQLite3 in Node.js.
MIT License
5.26k stars 391 forks source link

TypeError: o.default is not a constructor (vscode extension) #1071

Closed StephaneAdeso closed 9 months ago

StephaneAdeso commented 10 months ago

I have recently started developing an extension for VSCode. I just installed the better-sqlite3 dependency and I have followed its use as indicated in the documentation you have, but when it comes to building a new database, it gives me this error:

TypeError: o.default is not a constructor

After several hours searching and trying solutions proposed on Stackoverflow and here, I have not been able to solve it. So I decided to publish my own post on StackOverflow, but no one knows why this happens.

Below I leave you the link to the post I created in which all the code, dependencies, etc. appear. https://stackoverflow.com/questions/77144409/better-sqlite3-typeerror-o-default-is-not-a-constructor

If I need to re-copy all the information posted on StackOverflow here, let me know and I will be happy to do so.

Prinzhorn commented 10 months ago

It's always a good idea to create a minimal repro, e.g. set up a new TS project with just this code

import Database from 'better-sqlite3';
const db = new Database(':memory:');

(assuming new Database() is triggering the error, you didn't show a stack trace).

If that works, go from there and figure out what's wrong in your project. This is definitely not an issue with better-sqlite3 but some configuration issue with your bundling.

StephaneAdeso commented 10 months ago

There is no stack trace. If I remove the try/catch no error is shown.

I have uninstalled and reinstalled better-sqlite3 several times and used npm ci to clean the node modules and lock file. The funny thing is that after an installation it gave me the error: TypeError: Cannot read properties of undefined (reading 'indexOf') but after reinstalling it, it gave me the error again: TypeError: o.default is not a constructor

I do not want to use SQLite as an in-memory database but rather an embedded one.

At first instance it doesnt look like a project issue

StephaneAdeso commented 10 months ago

I changed the path to see if it was a permissions issue. I have selected my desktop (windows 11) and it still gives me the same error

StephaneAdeso commented 10 months ago

I just tried to create a file directly with 'fs' to check that I have permissions and that my project is not misconfigured. It has worked perfectly. The code is:

const dbPath = globalStoragePath + "\\" + name + ".db";
fs.writeFileSync(dbPath, '');
Prinzhorn commented 10 months ago

I never assumed it has anything to do with the filesystem at all. The code I provided was a mimimal example for you to test your build setup with a single ts file (no one is going to debug your plugin code for you). The error comes from a bundling issue, as I said earlier. Something with your webpack/ts and esm/commonjs interop is not configured properly. Here are some similar issue:

https://github.com/WiseLibs/better-sqlite3/issues/488 https://github.com/WiseLibs/better-sqlite3/issues/620 https://github.com/WiseLibs/better-sqlite3/issues/1055

I do not want to use SQLite as an in-memory database but rather an embedded one.

But that's how debugging works. You strip everything away (in your case two lines of code I provided are left) and then you see if it works. And then you keep working towards the code that breaks (adding more code, change the bundler config, use a non-memory db, etc.) until you find the point at which it breaks.

StephaneAdeso commented 10 months ago

no one is going to debug your plugin code for you

I was not expecting you to debug the code for me. i have been debugin for 2 days and didn't find any error message or info to follow. I just gave you the repo in case you needed it.

i soved it by adding to my webpack.config.js:

    externals: {
      "better-sqlite3": "commonjs better-sqlite3",
    },

thanks

Prinzhorn commented 10 months ago

I was not expecting you to debug the code for me. i have been debugin for 2 days and didn't find any error message or info to follow. I just gave you the repo in case you needed it.

I didn't mean it as sassy as it might have come across. But looking at your StackOverflow post there's so much code and config which is largely irrelevant (e.g. almost every single line of your package.json or most of BetterSqliteAdapter) that it creates a ton of noise for everyone trying to help you. That's why minimal repros are so important to get to the gist of the issue. And more often than not, when creating these minimal repros the issue reveals itself.

StephaneAdeso commented 10 months ago

I thought it had been solved with the webpack lines, but it turns out not. The database was generated with "fs" of node. When I delete it and try to generate it with better-sqlite3 it still gives me the error "cannot read properties of undefined (reading 'indexOf')." This is so frustrating. I will continue investigating to see what it could be.

StephaneAdeso commented 10 months ago

After many hours of research, at the moment it is not possible to directly use any library for SQLite when developing a vscode extension. At least if you want to work with local files instead of an in-memory database. This is because to use SQLite, each operating system must have different binaries. One of the possible solutions is to use this configuration in the webpack.config.js:

    externals: {
      "better-sqlite3": "commonjs better-sqlite3",
    },

But you will have to find a way for the user to have the necessary binaries installed on their computer.

sources of that information: