🎉 欢迎,我的Github主页.
解决nuxt、better-sqlite3安装报错问题

解决nuxt、better-sqlite3安装报错问题

报错信息

搜索到相关ISSUE: 看到是pnpm绑定错误

 ERROR  Cannot start nuxt:  Could not locate the bindings file. Tried:                                                                                                      15:58:33  
  D:\project\Demo-Nuxt\node_modules\.pnpm\better-sqlite3@12.4.1\node_modules\better-sqlite3\build\better_sqlite3.node
  D:\project\Demo-Nuxt\node_modules\.pnpm\better-sqlite3@12.4.1\node_modules\better-sqlite3\build\Debug\better_sqlite3.node
  D:\project\Demo-Nuxt\node_modules\.pnpm\better-sqlite3@12.4.1\node_modules\better-sqlite3\build\Release\better_sqlite3.node
  D:\project\Demo-Nuxt\node_modules\.pnpm\better-sqlite3@12.4.1\node_modules\better-sqlite3\out\Debug\better_sqlite3.node
  D:\project\Demo-Nuxt\node_modules\.pnpm\better-sqlite3@12.4.1\node_modules\better-sqlite3\Debug\better_sqlite3.node
  D:\project\Demo-Nuxt\node_modules\.pnpm\better-sqlite3@12.4.1\node_modules\better-sqlite3\out\Release\better_sqlite3.node
  D:\project\Demo-Nuxt\node_modules\.pnpm\better-sqlite3@12.4.1\node_modules\better-sqlite3\Release\better_sqlite3.node
  D:\project\Demo-Nuxt\node_modules\.pnpm\better-sqlite3@12.4.1\node_modules\better-sqlite3\build\default\better_sqlite3.node
  D:\project\Demo-Nuxt\node_modules\.pnpm\better-sqlite3@12.4.1\node_modules\better-sqlite3\compiled\22.20.0\win32\x64\better_sqlite3.node
  D:\project\Demo-Nuxt\node_modules\.pnpm\better-sqlite3@12.4.1\node_modules\better-sqlite3\addon-build\release\install-root\better_sqlite3.node
  D:\project\Demo-Nuxt\node_modules\.pnpm\better-sqlite3@12.4.1\node_modules\better-sqlite3\addon-build\debug\install-root\better_sqlite3.node
  D:\project\Demo-Nuxt\node_modules\.pnpm\better-sqlite3@12.4.1\node_modules\better-sqlite3\addon-build\default\install-root\better_sqlite3.node
  D:\project\Demo-Nuxt\node_modules\.pnpm\better-sqlite3@12.4.1\node_modules\better-sqlite3\lib\binding\node-v127-win32-x64\better_sqlite3.node

    at bindings (node_modules\.pnpm\bindings@1.5.0\node_modules\bindings\bindings.js:126:9)
    at new Database (node_modules\.pnpm\better-sqlite3@12.4.1\node_modules\better-sqlite3\lib\database.js:48:64)
    at getDB (/D:/project/Demo-Nuxt/node_modules/.pnpm/db0@0.3.2_better-sqlite3@12.4.1/node_modules/db0/dist/connectors/better-sqlite3.mjs:20:11)
    at Object.exec (/D:/project/Demo-Nuxt/node_modules/.pnpm/db0@0.3.2_better-sqlite3@12.4.1/node_modules/db0/dist/connectors/better-sqlite3.mjs:27:20)
    at getLocalDatabase (/D:/project/Demo-Nuxt/node_modules/.pnpm/@nuxt+content@3.7.1_better-sqlite3@12.4.1_magicast@0.3.5/node_modules/@nuxt/content/dist/module.mjs:35    at async processCollectionItems (/D:/project/Demo-Nuxt/node_modules/.pnpm/@nuxt+content@3.7.1_better-sqlite3@12.4.1_magicast@0.3.5/node_modules/@nuxt/content/dist/module.mjs:2976:14)
    at async /D:/project/Demo-Nuxt/node_modules/.pnpm/@nuxt+content@3.7.1_better-sqlite3@12.4.1_magicast@0.3.5/node_modules/@nuxt/content/dist/module.mjs:2953:20       
    at async initNuxt (/D:/project/Demo-Nuxt/node_modules/.pnpm/nuxt@4.1.3_@parcel+watcher@_07ff22dce285dd87314808f367da6350/node_modules/nuxt/dist/index.mjs:5859:3)   
    at async NuxtDevServer._load (/D:/project/Demo-Nuxt/node_modules/.pnpm/@nuxt+cli@3.29.3_magicast@0.3.5/node_modules/@nuxt/cli/dist/chunks/index.mjs:241:5)
    at async NuxtDevServer.load (/D:/project/Demo-Nuxt/node_modules/.pnpm/@nuxt+cli@3.29.3_magicast@0.3.5/node_modules/@nuxt/cli/dist/chunks/index.mjs:168:7)
    at async initialize (/D:/project/Demo-Nuxt/node_modules/.pnpm/@nuxt+cli@3.29.3_magicast@0.3.5/node_modules/@nuxt/cli/dist/chunks/index.mjs:470:3)
    at async Object.run (/D:/project/Demo-Nuxt/node_modules/.pnpm/@nuxt+cli@3.29.3_magicast@0.3.5/node_modules/@nuxt/cli/dist/chunks/dev.mjs:454:43)
    at async runCommand (/D:/project/Demo-Nuxt/node_modules/.pnpm/citty@0.1.6/node_modules/citty/dist/index.mjs:316:16)
    at async runCommand (/D:/project/Demo-Nuxt/node_modules/.pnpm/citty@0.1.6/node_modules/citty/dist/index.mjs:307:11)
    at async runMain (/D:/project/Demo-Nuxt/node_modules/.pnpm/citty@0.1.6/node_modules/citty/dist/index.mjs:445:7)

解决方案1

运行下方命令重新启动项目即可,此命令将添加相关onlyBuiltDependenciespackage.json

pnpm approve-builds

当然不嫌麻烦也可以手动添加

"onlyBuiltDependencies": [
  "@parcel/watcher",
  "@sentry/cli",
  "better-sqlite3",
  "esbuild",
  "sharp"
]

注:如果后续依然报错,那么尝试删掉node_modules后,重新运行

pnpm i --no-cache

解决方案2

编写pnpm-workspace.yaml,内容如下

onlyBuiltDependencies:
  - better-sqlite3

重新构建better-sqlite3

pnpm rebuild better-sqlite3