kit: importing sqlite3 fails in dev mode

Describe the bug

A recent update to sveltekit broke the usage of sqlite3 in server-side code.

When using npm’s sqlite3 module in server-side code and running the code in development mode, the import fails with

Cannot find module 'node_modules/sqlite3/package.json' imported from 'node_modules/node-pre-gyp/lib/pre-binding.js'
    at viteResolve (node_modules/vite/dist/node/chunks/dep-4a9cff06.js:67808:25)

Reproduction

use sqlite3 in a svelte hook, and run the code with svelte-kit dev

Reproducible in https://github.com/lovasoa/sanipasse/tree/5f4a3d5177c88462b2bee2c029eeb7534155cdb2

Logs

Error: Cannot find module '/home/olojkine/dev/sanipasse/node_modules/sqlite3/package.json' imported from '/home/olojkine/dev/sanipasse/node_modules/node-pre-gyp/lib/pre-binding.js'
    at viteResolve (/home/olojkine/dev/sanipasse/node_modules/vite/dist/node/chunks/dep-4a9cff06.js:67808:25)
    at Function._resolveFilename (/home/olojkine/dev/sanipasse/node_modules/vite/dist/node/chunks/dep-4a9cff06.js:67822:20)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.exports.find (/home/olojkine/dev/sanipasse/node_modules/node-pre-gyp/lib/pre-binding.js:20:23)
    at Object.<anonymous> (/home/olojkine/dev/sanipasse/node_modules/sqlite3/lib/sqlite3-binding.js:3:27)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)

System Info

System:
    OS: Linux 5.15 Fedora Linux 35 (Workstation Edition)
    CPU: (8) x64 Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
    Memory: 1.18 GB / 15.28 GB
    Container: Yes
    Shell: 5.1.8 - /bin/bash
  Binaries:
    Node: 16.13.0 - /usr/bin/node
    Yarn: 1.22.10 - /usr/bin/yarn
    npm: 8.1.0 - /usr/bin/npm
  Browsers:
    Firefox: 95.0
  npmPackages:
    @sveltejs/adapter-node: next => 1.0.0-next.56 
    @sveltejs/adapter-static: next => 1.0.0-next.22 
    @sveltejs/kit: next => 1.0.0-next.202 
    svelte: ^3.38.3 => 3.44.3 

Severity

blocking an upgrade

Additional Information

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 18 (13 by maintainers)

Commits related to this issue

Most upvoted comments

@bluwy Okay, knowing this load order issue, I could reproduce the issue in a minimal context :

https://github.com/lovasoa/sveltekit-bug-3118/

Closing this as vitejs/vite#6488 has been merged. Once that is released, you can update SvelteKit’s Vite version to that new version.

Closing this as vitejs/vite#6488 has been merged. Once that is released, you can update SvelteKit’s Vite version to that new version.

Hey, the problem still persists in the latest version of kit. I think it’s fair to close this issue only after vitejs version is updated on kit, since it’s not a peer dependency.

The core issue might be the same as #3053, but the stack trace looks new to me. I’m starting to think that Vite shouldn’t interfere with Node’s internal stuff, as you’ve found in vitejs/vite#3951. I haven’t got the bottom of fixing that yet, but I’m feeling we should revert that.