-
-
Notifications
You must be signed in to change notification settings - Fork 393
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
Using better-sqlite3
in a global CLI causes errors.
#1015
Comments
Please give more information. Which versions of NodeJS and better-sqlite3 are you using? In the end it does not matter if you install a project in the global context to use it as CLI or using it as a simple, local application. In any case better-sqlite3 requires a native module to be present. As long as you are using a NodeJS version, for which a prebuilt binary is present, it would automatically be downloaded when installing your module. If there is no fitting prebuilt binary available it will try to build the native module on demand, as you are observing on your system. |
Versions:
I think you misunderstand my issue - maybe as I asked two questions. The issue is that I am building my own CLI using Node, and when it is installed and run, This is an error, because when my CLI is installed with
My temp fix is to set the Node process CWD to |
The PR #974 should allow you to fix this issue easily in your code, this is basically the same issue that Electron sees, where paths are totally messed up. For normal Node.JS, everything tends to works great but in these edge cases where the native .node file will be in a different location, #974 will allow you to load it yourself and pass it in. ;-) |
Electron by itself does not mess up paths. It does not know about the lifecycle, packaging and special cases of each individual app using it. You have to make the paths work in your build process. Also, this particular issue is not related to Electron. After some hours of playing around with a custom module, installed as CLI, using another native module I would tend to say, the way loading modules in NodeJS is a bit quirky, especially if bindings (the module) is used too. The best solution so far was looking up the global module folder path and looking up the absolute path to the native module - I hope there is a better solution, but so far it does not seem this is an issue a library can solve every potential case of usage. |
Issue's with native I've got an entire build process (including app signing) for Windows and Linux that fixes all the above issues, but #974 will allow me to eliminate using a custom version of better-sqlite3 JS code. In better-sqlite3, if you use vite/rollup or webpack, the "require" is replaced with its own "require" function that doesn't understand how to load a .node file like is used here https://github.com/WiseLibs/better-sqlite3/blob/master/lib/database.js#L50 So then if you DON'T externalize in your bundler: I've customized my own copy of the better-sqlite3 -- I have the following change in my code which I pass into better-sqlite:
Then some custom code to the better sqlite database.js that uses
Using #974, I will be able to revert back to the stock better-sqlite library and eliminate the final better-sqlite3 electron issue that I've run into. Which is basically what you are running into, not having the proper path to the I've also had to do this same stuff with another module also (although that one I just forked and maintain my own copy as it never changes). By doing these steps I've saved probably 200-300ms in startup speed by eliminating all the extra extraction, disk searching hits, and finally eliminating loading each separate JS file since I only load a single bundled JS file for all database stuff. |
I have a Node.js CLI that I am developing that is installed with
npm install --global my-cli
This is in my package.json:
The issue is that when
my-cli
is run, it is run with from the current working directory where it is called.better-sqlite3
seems to try to read the SQLite compiled lib based on the current working directory, causing it to fail with these errors:I have a temporary work around where I set the current working directory before
new Database
and then restore it afterwards.What is the correct config for a global CLI that uses
better-sqlite3
?Also, how are prebuilt binaries used? On an M1 it seems to build SQLite from source, is this normal?
The text was updated successfully, but these errors were encountered: