Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mongodb in a SvelteKit project?

I imported mongodb with import { MongoClient } from 'mongodb';, (version of mongodb is 4.0.0-beta.5) it works fine in svelte-kit dev and svelte-kit preview, but when I try to start the server with node build, it throws Error: Dynamic require of "os" is not supported. How can I configure sveltekit or vite to import mongodb in production?

like image 590
Si Wei Yang Avatar asked Jan 30 '26 16:01

Si Wei Yang


1 Answers

I found that since vite only precompiles packages in dependencies (https://github.com/vitejs/vite/issues/162#issuecomment-629623566), mongodb should be in dependencies, not devDependencies.

like image 83
Si Wei Yang Avatar answered Feb 03 '26 10:02

Si Wei Yang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!