sveltekitmongodb-nodejs-driver

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?


Solution

  • 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.