-
-
Notifications
You must be signed in to change notification settings - Fork 398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Allow passing in the Require / or actual loaded "better_sqlite.node" #972
Comments
|
@JoshuaWise - Sorry, you misunderstood me. The issue I'm running into is:The So I am proposing a feature added to better-sqlite to either allow me to pass in the actual I'm willing to submit a PR for either feature if you are on board. I honestly think the second way (me being responsible to load and pass your code the already loaded I am aware I can exclude better-sqlite (& bindings & file-uri-to-path) node_modules to make sure I don't get the requires replaced. However, this is exponentially more costly in cpu/disk time. Each disk hit for each of the 14 separate files in better-sqlite, +2 for bindings, +2 for file-uri-to-path the is tremendously more expensive then having it all of those in a single minimized vendor.js packed file. |
@JoshuaWise would you prefer a PR for passing in the I assume if it is |
@mceachen - This DOES NOT fix my issue. PLEASE RE-OPEN. I'm actually using Vite/Rollup and it replaces the "require" also, but in a different way. The proposal I'm proposing is a lot more generic and not dependent upon which bundler people are using, and totally future safe... Either allow me to pass in the node "require" function, or I the dev can externally require the .node file and pass it in for usage.. ;-) (I prefer the second method, over the first, but either would solve the issue). Again, I'm willing to make a PR if you can tell me which way you prefer! |
@NathanaelA After giving this some thought, I think I apologize for taking so long to address this issue. Work and life have been quite busy. |
resolved: #974 |
@NathanaelA I am in the same situation. I need to rollup a library but when I do that and need the sqlite database type it throws the before mentioned error. Is there a workaround or do we have to wait until #974 is merged? |
@destyk If you have some time could you please explain if @NathanaelA and my problem are solved with your pull request? If the maintainers are busy I'll publish your pull request to npmjs so I can use it in etherpad in order to move forward with my rollup configuration. |
@SamTV12345 - #974 should fix the issue because you can then pre-load the native module from where you want to and just pass it in as As a minor aside, you do not need to create a whole new npm package & do a |
So nativebinding points to the path where the native binding is. Where do I get the native bindings? Sorry if this is a noob question I haven't really worked a lot with SQLite in JavaScript. And how could I do that platform independant? Yes. But once I need to do it in a CLI I can't use this method that is why. |
Everyone starts somewhere, so I never consider any question a noob question. I'll try to explain the entire process of what happens.
Certain Node NPM modules have c/c++ code that gets compiled into a native module (i.e. native bindings) for that specific OS platform. These native module/bindings Now normally that part is fairly seamless, the issue that I reported comes with a couple different corner cases:
In my case I actually have all three issues, I am using something other than webpack (rollup) and it does replace the So when I ran into this issue because when I packaged it in electron. (1) rollup replaced the require, (3)and the
You can't -
The build of the native bindings .node file are manual steps (for me I do have to kick them off and copy the files). They could be automated, but I only have to do it every couple months, so I don't bother adding all the work to automate it. These files after being build can be put into your repo, and then updates to them can also be pushed to your repo, keeping them in sync with the version of node/electron you are using for the project. In addition your custom version of the better-sqlite3 (until they finallly merge #974 ) can also be Hopefully that helps, if you need more info please feel free to email me. ;-) |
Thank you so much for taking the time to write such a long and detailed explanation. It is sad that there is not pure JavaScript implementation of SQLite but I guess this is not trivial with all the years of development and of course the speed that would degrade. I can now understand why there was a comment in the project I maintain with the content that SQLite should only be manually on an instance as a slight divergence in versions could easily crash that instance. The big question is how I'll implement that manual step elegant as now all the code is injected into the one javascript file. Maybe I'll put the SQLite code into a separate folder and require that from there. Anyways thanks again for the help. |
The easiest way I found to do that step is to have one of my build steps copy the file from my As for sqlite crashing, the only issue which I have personally run into is the linux glibc version issue, but that is why I use a custom docker (exactly like I use windows) to build all of my .node files that I then drop into my build resources folder, this way the .node files that are actually in my node_modules (and linked to glib 2.35) are never used for the final build, they are just for while I'm working on code and testing things... |
pass import { createRequire } from "module";
const nodeRequire = createRequire(import.meta.url); use |
I stumbled on this issue when I wanted to bundle a nodejs project which used better-sqlite3 with rollup. Lost quite a bit of time to figure it all out. Thanks @NathanaelA for reporting it and providing us with all the useful info and the fix. In order to make it work I had to do the following:
|
This is similar to #866 & #126
When webpacking (or in my case Rollup) and then the "bindings.js" attempts to look in the completely wrong location. I have found a hack that I think might be a better feature in the better-sqlite than me manually adding the code to the better-sqlite.
So here is the issue, I have the better_sqlite3.node copied to an extracted location in my electron application, however rollup (& webpack) replaces the "require" with their own require making it impossible for me to use the
nativeBinding
to point to it because a require just uses the replaced call. However, if replacebetter-sqlite3/lib/database.js
Lines 49 to 51 in 37b7714
the call inside the lib/database file with:
Then I can create my own "require" call that actually calls the normal node/electron require statement. (Webpack has I believe a
__non_webpack_require__
(I think it is called this) function which is the way you can access it in webpack). And you can also sometimes get access to it via global.require, etc...Now the feature idea or request is to allow me to pass in the
require
function call you will use for thenativeBinding
OR to allow me to manually load the binding myself and pass it in, so that you can just use it.The text was updated successfully, but these errors were encountered: