WiseLibs / better-sqlite3

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

Error: Could not locate the bindings file. #146

Closed jackmerrill closed 5 years ago

jackmerrill commented 6 years ago

I'm trying to get my Discord.JS bot onto an AWS server, and I keep getting this error: /home/ec2-user/EcoBot/node_modules/bindings/bindings.js:96 throw err ^

Error: Could not locate the bindings file. Tried: → /home/ec2-user/EcoBot/node_modules/better-sqlite3/build/better_sqlite3.node → /home/ec2-user/EcoBot/node_modules/better-sqlite3/build/Debug/better_sqlite3.node → /home/ec2-user/EcoBot/node_modules/better-sqlite3/build/Release/better_sqlite3.node → /home/ec2-user/EcoBot/node_modules/better-sqlite3/out/Debug/better_sqlite3.node → /home/ec2-user/EcoBot/node_modules/better-sqlite3/Debug/better_sqlite3.node → /home/ec2-user/EcoBot/node_modules/better-sqlite3/out/Release/better_sqlite3.node → /home/ec2-user/EcoBot/node_modules/better-sqlite3/Release/better_sqlite3.node → /home/ec2-user/EcoBot/node_modules/better-sqlite3/build/default/better_sqlite3.node → /home/ec2-user/EcoBot/node_modules/better-sqlite3/compiled/8.11.1/linux/x64/better_sqlite3.node at bindings (/home/ec2-user/EcoBot/node_modules/bindings/bindings.js:93:9) at Object. (/home/ec2-user/EcoBot/node_modules/better-sqlite3/lib/database.js:4:40) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at Object. (/home/ec2-user/EcoBot/node_modules/better-sqlite3/index.js:2:18)

I've tried looking at other issues and tried reinstalling better-sqlite3, but that didn't help. I moved the files from my Mac to the server. What do I do?

mandaputtra commented 6 years ago

Thats not a problem of the better-sqlite3. I had this problem before.

You can fix it by follow trouble shooting guides in here

Thats a problem with node-sass i dont know if it was dependecy in better-sqlite3 here.

If still the trouble shooting guides doesn`t give you any solution. You can try my step.

  1. Remove node-module and package-lock.json 2.npm install

If still not.

  1. Remove node-module and package-lock.json
  2. Unninstall your node completly with globlal modules you have install.
  3. Install again.

It sould be working now.

jackmerrill commented 6 years ago

No.. That didn't work. I tried both methods and still got the same result.

mandaputtra commented 6 years ago

Do you clearly uninstall all node with its global module? What OS do you use if in Mac or GNU/Linux i highly recommend to use NVM.

Dont know if you use windows.

jackmerrill commented 6 years ago

I uninstalled node (afaik) and reinstalled it. I'm using Amazon Linux which I believe is Fedora, but I might be wrong.

mandaputtra commented 6 years ago

Or you can try to rebuild the node-sass since better-sqlite3 are use it for it dependency. (This library are write on C++/C)

It should be fixed soon or explained soon by the author since not just one or two person had this issue.

Hope it will be better soon.

jackmerrill commented 6 years ago

How do I do that?

JoshuaWise commented 6 years ago

@yoshifan509 After installing better-sqlite3, could you print out the (nested) contents of your node_modules/better-sqlite3/build directory? That will help me debug your issue.

jackmerrill commented 6 years ago

@yoshifan509 After installing better-sqlite3, could you print out the (nested) contents of your node_modules/better-sqlite3/build directory? That will help me debug your issue.

Not trying to be rude or anything, but if it took you 2 months to respond to that, after I switched to a different SQLite Node.JS package, you shouldn't ask for the printed contents of a directory :)

JoshuaWise commented 6 years ago

@yoshifan509 You are correct.

This issue has been reported by many people for a long time, and I have never been able to reproduce it. After a certain point, one starts paying less attention to the same report over and over, hoping that another member of the community (who can reproduce the error) might be able to help.

Last night I suddenly had an idea about what might be causing this issue, which is why I chimed in. But I would need to see the directory to confirm my theory. Sorry for the trouble. Or perhaps someone else could provide me with the same information.

Noxillio commented 6 years ago

@JoshuaWise

I managed to fix it by making sure the "build" folder doesn't get deleted when better-sqlite3 is done installing. I believe that was your theory? I noticed it wasn't there so I uninstalled, then deleted the better-sqlite3 folder and tried again, then the folder stayed and it started working fine.

JoshuaWise commented 5 years ago

@kgossiaux That wasn't my theory. I wonder why that was happening for you, and I wonder if other people's issue was caused by the same thing.

Noxillio commented 5 years ago

@JoshuaWise It worked for me really well, for some odd reason every time I tried to install, that folder wasn't there so I got the same issue.

JoshuaWise commented 5 years ago

This should be fixed in version 5.0.0.

IdioticFruit commented 3 years ago

This should be fixed in version 5.0.0.

Although I installed the latest version, its the same thing with the same error

[2020-11-23 21:08:55]: ERROR Uncaught Exception: Error: Could not locate the bindings file. Tried: → C:\Users\pn965\Downloads\michelle\node_modules\enmap\node_modules\better-sqlite3\build\better_sqlite3.node → C:\Users\pn965\Downloads\michelle\node_modules\enmap\node_modules\better-sqlite3\build\Debug\better_sqlite3.node → C:\Users\pn965\Downloads\michelle\node_modules\enmap\node_modules\better-sqlite3\build\Release\better_sqlite3.node → C:\Users\pn965\Downloads\michelle\node_modules\enmap\node_modules\better-sqlite3\out\Debug\better_sqlite3.node → C:\Users\pn965\Downloads\michelle\node_modules\enmap\node_modules\better-sqlite3\Debug\better_sqlite3.node → C:\Users\pn965\Downloads\michelle\node_modules\enmap\node_modules\better-sqlite3\out\Release\better_sqlite3.node → C:\Users\pn965\Downloads\michelle\node_modules\enmap\node_modules\better-sqlite3\Release\better_sqlite3.node → C:\Users\pn965\Downloads\michelle\node_modules\enmap\node_modules\better-sqlite3\build\default\better_sqlite3.node → C:\Users\pn965\Downloads\michelle\node_modules\enmap\node_modules\better-sqlite3\compiled\14.15.1\win32\x64\better_sqlite3.node → C:\Users\pn965\Downloads\michelle\node_modules\enmap\node_modules\better-sqlite3\addon-build\release\install-root\better_sqlite3.node → C:\Users\pn965\Downloads\michelle\node_modules\enmap\node_modules\better-sqlite3\addon-build\debug\install-root\better_sqlite3.node → C:\Users\pn965\Downloads\michelle\node_modules\enmap\node_modules\better-sqlite3\addon-build\default\install-root\better_sqlite3.node → C:\Users\pn965\Downloads\michelle\node_modules\enmap\node_modules\better-sqlite3\lib\binding\node-v83-win32-x64\better_sqlite3.node at bindings (C:\Users\pn965\Downloads\michelle\node_modules\bindings\bindings.js:126:9) at Object. (C:\Users\pn965\Downloads\michelle\node_modules\enmap\node_modules\better-sqlite3\lib\database.js:9:24) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Module.require (internal/modules/cjs/loader.js:952:19) at require (internal/modules/cjs/helpers.js:88:18) at Object. (C:\Users\pn965\Downloads\michelle\node_modules\enmap\node_modules\better-sqlite3\lib\index.js:2:18) at Module._compile (internal/modules/cjs/loader.js:1063:30)

marcj commented 3 years ago

Got the same error. Just installed my packages (one with a dependency to better-sqlite3 v7.1.1.) using Lerna on a fresh Ubuntu 20 using Node v14 and it seems it didn't build anything:

$ ls -al packages/sql/node_modules/better-sqlite3/
total 68
drwxrwxr-x  9 parallels parallels 4096 Dec 13 01:13 .
drwxrwxr-x 97 parallels parallels 4096 Dec 13 01:18 ..
-rw-rw-r--  1 parallels parallels   28 Oct 26  1985 .gitattributes
drwxrwxr-x  2 parallels parallels 4096 Dec 13 01:13 .github
-rw-rw-r--  1 parallels parallels 2040 Oct 26  1985 .travis.yml
-rw-rw-r--  1 parallels parallels 1078 Oct 26  1985 LICENSE
-rw-rw-r--  1 parallels parallels 4792 Oct 26  1985 README.md
-rw-rw-r--  1 parallels parallels  255 Oct 26  1985 appveyor.yml
drwxrwxr-x  3 parallels parallels 4096 Dec 13 01:13 benchmark
-rw-rw-r--  1 parallels parallels  620 Oct 26  1985 binding.gyp
drwxrwxr-x  2 parallels parallels 4096 Dec 13 01:13 deps
drwxrwxr-x  2 parallels parallels 4096 Dec 13 01:13 docs
drwxrwxr-x  2 parallels parallels 4096 Dec 13 01:13 lib
-rw-rw-r--  1 parallels parallels 2381 Dec 13 01:13 package.json
drwxrwxr-x  4 parallels parallels 4096 Dec 13 01:13 src
drwxrwxr-x  2 parallels parallels 4096 Dec 13 01:13 test

It might be that some dependencies (a fresh Ubuntu installation here) are missing and the build has silently failed?

marcj commented 3 years ago

After removing the package rm -rf packages/sql/node_modules/better-sqlite3 and re-installing everything using lerna bootstrap everything works. Don't know why it hasn't been installed correctly at the beginning 🤷‍♂️.

IdioticFruit commented 3 years ago

hope the developers find a solution to this

After removing the package rm -rf packages/sql/node_modules/better-sqlite3 and re-installing everything using lerna bootstrap everything works. Don't know why it hasn't been installed correctly at the beginning 🤷‍♂️.

mandaputtra commented 3 years ago

@marcj You might missing node-gyp at first install

marcj commented 3 years ago

@mandaputtra I didn't install anything in Ubuntu between the first install and the second npm install.

twigz20 commented 3 years ago

I got it working by simply using Node: v14.15.0. On my machine (Linux Mint 20) it always fails with any node version higher than that.

4KSDXD commented 3 years ago

This should be fixed in version 5.0.0.

@yoshifan509 After installing better-sqlite3, could you print out the (nested) contents of your node_modules/better-sqlite3/build directory? That will help me debug your issue.

I have the same issue, ca you help me fix it. I've been stuck trying to fix this for over a week now, if you have a Discord, dm me at GentWorm#8094

meropis commented 3 years ago

^ solution in this thread - note: this bug was NOT fixed in v5.0

BinarySpike commented 2 years ago

I caused this error by navigating into node_modules\better-sqlite3 and executing yarn build-debug before implementing the workaround in https://github.com/JoshuaWise/better-sqlite3/issues/713

Deleting my node_modules folder and running yarn again resolved the issue.

KarlGong commented 2 years ago

@JoshuaWise I can reproduce it in Tencent Cloud Serverless function. When I upload the code and enable the auto install dependancy, the issue appears. Following is the better-sqlite3 package in the Serverless function.

image

I compared with my local node_modules, the node_modules/better-sqlite3/build is missed.

node version: 16.13 better-sqlite3 version: 7.5.0

KarlGong commented 2 years ago

Following is the log:

START RequestId: 3e9fab09e08a319efae8a92811c45515

/var/user/node_modules/bindings/bindings.js:135

throw err;

^

Error: Could not locate the bindings file. Tried:

→ /var/user/node_modules/better-sqlite3/build/better_sqlite3.node

→ /var/user/node_modules/better-sqlite3/build/Debug/better_sqlite3.node

→ /var/user/node_modules/better-sqlite3/build/Release/better_sqlite3.node

→ /var/user/node_modules/better-sqlite3/out/Debug/better_sqlite3.node

→ /var/user/node_modules/better-sqlite3/Debug/better_sqlite3.node

→ /var/user/node_modules/better-sqlite3/out/Release/better_sqlite3.node

→ /var/user/node_modules/better-sqlite3/Release/better_sqlite3.node

→ /var/user/node_modules/better-sqlite3/build/default/better_sqlite3.node

→ /var/user/node_modules/better-sqlite3/compiled/14.18.2/linux/x64/better_sqlite3.node

→ /var/user/node_modules/better-sqlite3/addon-build/release/install-root/better_sqlite3.node

→ /var/user/node_modules/better-sqlite3/addon-build/debug/install-root/better_sqlite3.node

→ /var/user/node_modules/better-sqlite3/addon-build/default/install-root/better_sqlite3.node

→ /var/user/node_modules/better-sqlite3/lib/binding/node-v83-linux-x64/better_sqlite3.node

at bindings (/var/user/node_modules/bindings/bindings.js:126:9)

at new Database (/var/user/node_modules/better-sqlite3/lib/database.js:48:64)

at Object.<anonymous> (/var/user/app.js:24:12)

at Module._compile (internal/modules/cjs/loader.js:1085:14)

at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)

at Module.load (internal/modules/cjs/loader.js:950:32)

at Function.Module._load (internal/modules/cjs/loader.js:790:12)

at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)

at internal/main/run_main_module.js:17:47 {

tries: [

'/var/user/node_modules/better-sqlite3/build/better_sqlite3.node',

'/var/user/node_modules/better-sqlite3/build/Debug/better_sqlite3.node',

'/var/user/node_modules/better-sqlite3/build/Release/better_sqlite3.node',

'/var/user/node_modules/better-sqlite3/out/Debug/better_sqlite3.node',

'/var/user/node_modules/better-sqlite3/Debug/better_sqlite3.node',

'/var/user/node_modules/better-sqlite3/out/Release/better_sqlite3.node',

'/var/user/node_modules/better-sqlite3/Release/better_sqlite3.node',

'/var/user/node_modules/better-sqlite3/build/default/better_sqlite3.node',

'/var/user/node_modules/better-sqlite3/compiled/14.18.2/linux/x64/better_sqlite3.node',

'/var/user/node_modules/better-sqlite3/addon-build/release/install-root/better_sqlite3.node',

'/var/user/node_modules/better-sqlite3/addon-build/debug/install-root/better_sqlite3.node',

'/var/user/node_modules/better-sqlite3/addon-build/default/install-root/better_sqlite3.node',

'/var/user/node_modules/better-sqlite3/lib/binding/node-v83-linux-x64/better_sqlite3.node'

]

}

Init Report RequestId: 3e9fab09e08a319efae8a92811c45515 Coldstart: 587ms (PullCode: 186ms InitRuntime: 1ms InitFunction: 400ms) Memory: 64MB MemUsage: 0.13MB

ERROR RequestId: 3e9fab09e08a319efae8a92811c45515 Result: {"errorCode": -1, "errorMessage": "Failed to initialize the container. Please confirm that the container can be started locally.", "statusCode": 405}

END RequestId: 3e9fab09e08a319efae8a92811c45515

Report RequestId: 3e9fab09e08a319efae8a92811c45515 Duration: 0ms Memory: 64MB MemUsage: 0.13MB

amrit-2006 commented 2 years ago

I think this error occurs only in cloud hosts. This error does not occur in local hosts. So it has something to do with file permissions maybe

stefandesu commented 2 years ago

I have this issue on my M1 MacBook Air. I feel like I've tried everything and couldn't get it to work. Same issue with sqlite/sqlite3.

camw0 commented 2 years ago

+1 to the comment by amrit, I'm not getting this issue locally, but only when running a project on a VPS in Docker.

stefandesu commented 2 years ago

I was able to fix the issue on the M1 MBA by manually running the build script:

npm install better-sqlite3
ls node_modules/better-sqlite3 # folder `build` does not exist, hence the error messages about missing bindings
cd node_modules/better-sqlite3
npm run build-release
ls build # now exists 🥳
cd ../..

This wasn't necessary on my Intel iMac, so I'm not sure where the issue is. 🤔

broccolihighkicks commented 2 years ago

I am getting this error on a Mac M1, Node v18.

Deleting node_modules and reinstalling does not work, neither does npm run build-release. node_modules/better-sqlite3/build does not exist.

@JoshuaWise Is there a fix or workaround for this? Can you place it in the troubleshooting md?

gamanox commented 1 year ago

Try installing XCode and open it then run again the npm/yarn install process @broccolihighkicks

Himesh512 commented 1 year ago

No.. That didn't work. I tried both methods and still got the same result.

i tried both twice first time remove project from project setup, and second time remove project from local project and user location both.

same thing got worked for other machine not for the current machine which i'm working

zack commented 1 year ago

I'm having the same issue on a DigitalOcean droplet running Ubuntu 12.04LTS.

After an error-free npm install on the droplet:

$ ls node_modules/better-sqlite3/build/Release
obj  obj.target

After an error-free npm install on my local machine:

$ ls node_modules/better-sqlite3/build/Release
better_sqlite3.node  obj  obj.target  sqlite3.a  test_extension.node

I attempted to run npm run build-release inside of the Release directory but it failed. Searching the failure led me to another issue on this very project, where a commenter noted that that failure was likely due to a resource error.

At that point I nuked node_modules, bumped my DigitalOcean droplet from 512MB/10GB to 1GB/10GB, reran npm install, and now everything runs smoothly and the files are present as expected:

$ ls node_modules/better-sqlite3/build/Release/
better_sqlite3.node  obj  obj.target  sqlite3.a  test_extension.node
noam-honig commented 1 year ago

Had the same issue - tried build release and got an error that python is not installed, installed python and ran npm i again and it solved the problem

untidy-hair commented 10 months ago

This might help people using yarn. After I saw this error, I ran yarn add --dev better-sqlite3 and the issue is gone.

$ ls node_modules/better-sqlite3/build/Release
ls: node_modules/better-sqlite3/build/Release: No such file or directory
$ yarn add --dev better-sqlite3
... snip ...
$ ls node_modules/better-sqlite3/build/Release
better_sqlite3.node
bhatvikrant commented 8 months ago

I was able to fix the issue on the M1 MBA by manually running the build script:

npm install better-sqlite3
ls node_modules/better-sqlite3 # folder `build` does not exist, hence the error messages about missing bindings
cd node_modules/better-sqlite3
npm run build-release
ls build # now exists 🥳
cd ../..

This wasn't necessary on my Intel iMac, so I'm not sure where the issue is. 🤔

This worked for me! on my M1 MBP

jvzaniolo commented 6 months ago

Having the same issue on my M1 MBP. I've tried with both node v18 and v20.

/Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/dist/server.js:232
      throw err;
      ^

Error: Could not locate the bindings file. Tried:
 → /Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/build/better_sqlite3.node
 → /Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/build/Debug/better_sqlite3.node
 → /Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/build/Release/better_sqlite3.node
 → /Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/out/Debug/better_sqlite3.node
 → /Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/Debug/better_sqlite3.node
 → /Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/out/Release/better_sqlite3.node
 → /Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/Release/better_sqlite3.node
 → /Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/build/default/better_sqlite3.node
 → /Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/compiled/20.11.1/darwin/arm64/better_sqlite3.node
 → /Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/addon-build/release/install-root/better_sqlite3.node
 → /Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/addon-build/debug/install-root/better_sqlite3.node
 → /Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/addon-build/default/install-root/better_sqlite3.node
 → /Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/lib/binding/node-v115-darwin-arm64/better_sqlite3.node
    at bindings (/Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/dist/server.js:226:13)
    at new Database2 (/Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/dist/server.js:843:69)
    at Object.<anonymous> (/Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/dist/server.js:931:11)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49 {
  tries: [
    '/Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/build/better_sqlite3.node',
    '/Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/build/Debug/better_sqlite3.node',
    '/Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/build/Release/better_sqlite3.node',
    '/Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/out/Debug/better_sqlite3.node',
    '/Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/Debug/better_sqlite3.node',
    '/Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/out/Release/better_sqlite3.node',
    '/Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/Release/better_sqlite3.node',
    '/Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/build/default/better_sqlite3.node',
    '/Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/compiled/20.11.1/darwin/arm64/better_sqlite3.node',
    '/Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/addon-build/release/install-root/better_sqlite3.node',
    '/Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/addon-build/debug/install-root/better_sqlite3.node',
    '/Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/addon-build/default/install-root/better_sqlite3.node',
    '/Users/jvzaniolo/Developer/nodejs/02-desafio-daily-diet/lib/binding/node-v115-darwin-arm64/better_sqlite3.node'
  ]
}

Node.js v20.11.1

Uninstalling and installing node didn't work. Running the build command didn't work.

Edit:

@JoshuaWise node is looking for better_sqlite3.node file anywhere but inside the node_modules/better-sqlite3 folder.

I was able to run my server by copying and pasting the build folder from node_modules/better-sqlite3 to my project's root directory.

I don't know how you can fix it but I hope that can give you more context

Edit 2:

Using a different package manager like pnpm worked for me.

deadcoder0904 commented 6 months ago

i faced this issue in docker on windows 11 using wsl2 with ubuntu as a distro suddenly. i was just moving copy statements in dockerfile. i did nothing to touch it. i even cleared all my cache, removed node_modules, reinstalled it both on my local computer as well as docker.

i literally tried everything here but nothing worked. i also tried doing npm run build-release command after i exec into my container but it yelled i didn't have python. i could install python but then i'd have to do it everytime or worse add it to my image.

my simpler solution was to install better-sqlite3 inside Dockerfile as it is a native node.js module & remove it from package.json. the caveat to this is it won't work on local without docker but anyone can add it with pnpm i better-sqlite3 again if they want to.

Dockerfile

# Install native node_modules in Dockerfile so it uses pnpm's supportedArchitectures feature in package.json. sharp is required only if you use next/image optimization while better-sqlite3 is used in the app itself
RUN pnpm install sharp@0.33.2 better-sqlite3@9.4.3

package.json

{
    "dependencies": {
    "@ngneat/falso": "^7.2.0",
    "@t3-oss/env-nextjs": "^0.9.2",
    "drizzle-orm": "^0.30.1",
    "jiti": "^1.21.0",
    "next": "14.1.3",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "std-env": "^3.7.0",
    "zod": "^3.22.4"
  },
  "devDependencies": {
    "@types/better-sqlite3": "^7.6.9",
    "@types/node": "^20.11.27",
    "@types/react": "^18.2.65",
    "@types/react-dom": "^18.2.22",
    "dotenv": "^16.4.5",
    "drizzle-kit": "^0.20.14",
    "knip": "^5.0.4",
    "rimraf": "^5.0.5",
    "tsx": "^4.7.1",
    "typescript": "^5.4.2"
  },
  "pnpm": {
    "supportedArchitectures": {
      "os": [
        "current"
      ],
      "cpu": [
        "x64",
        "arm64"
      ]
    }
  }
}

the full repo is at https://github.com/deadcoder0904/easypanel-nextjs-sqlite if you wanna reference it.

edit: the above solution stopped working randomly

the new solution:

Dockerfile

RUN apk add --update --no-cache python3 build-base gcc && ln -sf /usr/bin/python3 /usr/bin/python
RUN cd node_modules/better-sqlite3 && pnpm build-release
Full dockerfile for reference ### Dockerfile ```dockerfile FROM node:20-alpine AS base # mostly inspired from https://github.com/BretFisher/node-docker-good-defaults/blob/main/Dockerfile & https://github.com/remix-run/example-trellix/blob/main/Dockerfile # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. RUN apk add --no-cache libc6-compat RUN corepack enable && corepack prepare pnpm@8.15.4 --activate # 1. Install all dependencies including devDependencies FROM base AS development # Root user is implicit so you don't have to actually specify it. From https://stackoverflow.com/a/45553149/6141587 # USER root USER node # WORKDIR now sets correct permissions if you set USER first so `USER node` has permissions on `/app` directory WORKDIR /app # set the store dir to a folder that is not in the project RUN pnpm config set store-dir ~/.pnpm-store # Install dependencies based on the preferred package manager COPY --chown=node:node package.json pnpm-lock.yaml ./ RUN --mount=type=cache,id=pnpm,target=~/.pnpm-store pnpm fetch COPY --chown=node:node . . ENV NODE_ENV development ENV NEXT_TELEMETRY_DISABLED 1 USER node RUN --mount=type=cache,id=pnpm,target=~/.pnpm-store pnpm install --frozen-lockfile --recursive --prefer-offline # Install native node_modules in Dockerfile so it uses pnpm's supportedArchitectures feature in package.json. sharp is required only if you use next/image optimization while better-sqlite3 is used in the app itself RUN --mount=type=cache,id=pnpm,target=~/.pnpm-store pnpm install sharp@0.33.2 better-sqlite3@9.4.3 --recursive --prefer-offline # Copied from https://stackoverflow.com/a/69867550/6141587 USER root # Give /data directory correct permissions otherwise WAL mode won't work. It means you can't have 2 users writing to the database at the same time without this line as *.sqlite-wal & *.sqlite-shm are automatically created & deleted when *.sqlite is busy. RUN mkdir -p /data && chown -R node:node /data RUN apk add --update --no-cache python3 build-base gcc && ln -sf /usr/bin/python3 /usr/bin/python RUN cd node_modules/better-sqlite3 && pnpm build-release USER node # Copy better_sqlite3 build for current platform # RUN cp /app/node_modules/better-sqlite3/build/Release/better_sqlite3.node /app/_node_modules/better-sqlite3/build/Release/better_sqlite3.node # 2. Rebuild the source code only when needed FROM base AS builder WORKDIR /app # Inspired by https://github.com/vercel/next.js/discussions/36935 RUN mkdir -p /app/.next/cache && chown -R node:node /app/.next/cache # Persist the next cache in a volume VOLUME ["/app/.next/cache"] COPY --from=development --chown=node:node /app/node_modules ./node_modules COPY --chown=node:node . . # This will do the trick, use the corresponding env file for each environment. COPY --chown=node:node .env* ./ # set to production otherwise it throws error ⚠ You are using a non-standard "NODE_ENV" value in your environment. This creates inconsistencies in the project and is strongly advised against. Read more: https://nextjs.org/docs/messages/non-standard-node-env ENV NODE_ENV production ENV NEXT_TELEMETRY_DISABLED 1 RUN pnpm build # 3. Production image, copy all the files and run next FROM base AS runner USER node WORKDIR /app EXPOSE 3000 ENV PORT 3000 ENV HOSTNAME 0.0.0.0 ENV NODE_ENV production ENV NEXT_TELEMETRY_DISABLED 1 COPY --from=builder --chown=node:node /app/.env* ./ COPY --from=builder --chown=node:node /app/public ./public # Automatically leverage output traces to reduce image size # https://nextjs.org/docs/advanced-features/output-file-tracing COPY --from=builder --chown=node:node /app/.next/standalone ./ COPY --from=builder --chown=node:node /app/.next/static ./.next/static # Move the drizzle directory to the runtime image COPY --from=builder --chown=node:node /app/src/app/db/migrations ./migrations # Move the litestream binary to the runtime image from the litestream image # You can use a specific version of litestream by changing the tag # COPY --from=litestream/litestream:0.3.13 /usr/local/bin/litestream /usr/local/bin/litestream COPY --from=litestream/litestream:latest --chown=node:node /usr/local/bin/litestream /usr/local/bin/litestream COPY --from=builder --chown=node:node /app/litestream.yml /etc/litestream.yml COPY --from=builder --chown=node:node /app/scripts ./scripts ```

but i think the one-liner above in package.json is simpler:

"postinstall": "cp -R node_modules/better-sqlite3/build ."

even though that didn't work for me.

this worked finally i reinstalled `better-sqlite3` in `package.json` & removed it from `dockerfile`. its weird bcz it sometimes doesn't work lol. ### Dockerfile ```dockerfile FROM node:20-alpine AS base # mostly inspired from https://github.com/BretFisher/node-docker-good-defaults/blob/main/Dockerfile & https://github.com/remix-run/example-trellix/blob/main/Dockerfile # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. RUN apk add --no-cache libc6-compat RUN corepack enable && corepack prepare pnpm@8.15.4 --activate # 1. Install all dependencies including devDependencies FROM base AS development # Root user is implicit so you don't have to actually specify it. From https://stackoverflow.com/a/45553149/6141587 # USER root USER node # WORKDIR now sets correct permissions if you set USER first so `USER node` has permissions on `/app` directory WORKDIR /app # set the store dir to a folder that is not in the project RUN pnpm config set store-dir ~/.pnpm-store # Install dependencies based on the preferred package manager COPY --chown=node:node package.json pnpm-lock.yaml ./ RUN --mount=type=cache,id=pnpm,target=~/.pnpm-store pnpm fetch COPY --chown=node:node . . ENV NODE_ENV development ENV NEXT_TELEMETRY_DISABLED 1 # Copied from https://stackoverflow.com/a/69867550/6141587 USER root # Give /data directory correct permissions otherwise WAL mode won't work. It means you can't have 2 users writing to the database at the same time without this line as *.sqlite-wal & *.sqlite-shm are automatically created & deleted when *.sqlite is busy. RUN mkdir -p /data && chown -R node:node /data USER node RUN --mount=type=cache,id=pnpm,target=~/.pnpm-store pnpm install --frozen-lockfile --recursive --prefer-offline # Install native node_modules in Dockerfile so it uses pnpm's supportedArchitectures feature in package.json. sharp is required only if you use next/image optimization RUN --mount=type=cache,id=pnpm,target=~/.pnpm-store pnpm install sharp@0.33.2 --recursive --prefer-offline # 2. Rebuild the source code only when needed FROM base AS builder WORKDIR /app # Inspired by https://github.com/vercel/next.js/discussions/36935 RUN mkdir -p /app/.next/cache && chown -R node:node /app/.next/cache # Persist the next cache in a volume VOLUME ["/app/.next/cache"] COPY --from=development --chown=node:node /app/node_modules ./node_modules COPY --chown=node:node . . # This will do the trick, use the corresponding env file for each environment. COPY --chown=node:node .env* ./ # set to production otherwise it throws error ⚠ You are using a non-standard "NODE_ENV" value in your environment. This creates inconsistencies in the project and is strongly advised against. Read more: https://nextjs.org/docs/messages/non-standard-node-env ENV NODE_ENV production ENV NEXT_TELEMETRY_DISABLED 1 RUN pnpm build # 3. Production image, copy all the files and run next FROM base AS runner USER node WORKDIR /app EXPOSE 3000 ENV PORT 3000 ENV HOSTNAME 0.0.0.0 ENV NODE_ENV production ENV NEXT_TELEMETRY_DISABLED 1 COPY --from=builder --chown=node:node /app/.env* ./ COPY --from=builder --chown=node:node /app/public ./public # Automatically leverage output traces to reduce image size # https://nextjs.org/docs/advanced-features/output-file-tracing COPY --from=builder --chown=node:node /app/.next/standalone ./ COPY --from=builder --chown=node:node /app/.next/static ./.next/static # Move the drizzle directory to the runtime image COPY --from=builder --chown=node:node /app/src/app/db/migrations ./migrations # Move the litestream binary to the runtime image from the litestream image # You can use a specific version of litestream by changing the tag # COPY --from=litestream/litestream:0.3.13 /usr/local/bin/litestream /usr/local/bin/litestream COPY --from=litestream/litestream:latest --chown=node:node /usr/local/bin/litestream /usr/local/bin/litestream COPY --from=builder --chown=node:node /app/litestream.yml /etc/litestream.yml COPY --from=builder --chown=node:node /app/scripts ./scripts ```
stabildev commented 3 months ago

Facing the same issue with bun and drizzle-kit. bunx drizzle-kit migrate would produce the error Could not locate the bindings file.

Working for me:

bun add -D node-gyp
cd node_modules/better-sqlite3
bun run build-release
cd -

Now bunx drizzle-kit migrate works.

Hope this helps

coltossoff commented 2 months ago

The "Could not locate the bindings file" error is not a "node" issue and can't be fixed with npm installs alone. First to explain what it is; sqlite3, etc. are separate applications that need to be install on a system, and the node better-sqlite, sqlite3, etc libraries are simply a means to connect to those applications.

You probably have to install sqlite on your server/local machine to get your application working. For AWS linux I think it is yum install sqlite3.

I really wish the library maintainers would come up with better errors for this, but this is where we are. Hope this helps.

keisanng commented 1 month ago

Still having this error locally on Linux trying to use bun, npm and manually running build-release. SQlite3 is also installed locally, and Bun SQlite works perfectly in place of better-sqlite3 in Drizzle ORM.