I just got started with svelte and was trying to make an app with redis as the db. I made a typescript file with all the db functions I would need and tried to import it into my svelte components, but when I did that I got the following error
Class extends value undefined is not a constructor or null
TypeError: Class extends value undefined is not a constructor or null
at node_modules/@node-redis/client/dist/lib/client/socket.js (http://localhost:3000/node_modules/.vite/chunk-L35TFNQI.js?v=60c87e0f:6515:46)
at __require (http://localhost:3000/node_modules/.vite/chunk-VP3FZ6LR.js?v=60c87e0f:25:44)
at node_modules/@node-redis/client/dist/lib/client/index.js (http://localhost:3000/node_modules/.vite/chunk-L35TFNQI.js?v=60c87e0f:9192:20)
at __require (http://localhost:3000/node_modules/.vite/chunk-VP3FZ6LR.js?v=60c87e0f:25:44)
at node_modules/@node-redis/client/dist/index.js (http://localhost:3000/node_modules/.vite/redis.js?v=60c87e0f:852:20)
at __require (http://localhost:3000/node_modules/.vite/chunk-VP3FZ6LR.js?v=60c87e0f:25:44)
at node_modules/redis/dist/index.js (http://localhost:3000/node_modules/.vite/redis.js?v=60c87e0f:2589:20)
at __require (http://localhost:3000/node_modules/.vite/chunk-VP3FZ6LR.js?v=60c87e0f:25:44)
at http://localhost:3000/node_modules/.vite/redis.js?v=60c87e0f:2615:21
This is my redis file (even with only this much, I get the same error)
import redis from 'redis'
export var str = "sample string"
This is my svelte component’s script
<script lang="ts">
import { str } from "../redis_test";
</script>
2
Answers
Normally, because the redis – npm package is designed for Node.js, I would say to follow How to use npm modules in browser? and use
browserify
so that it’s able to run in the browser.After reading Can I connect directly to a Redis server from JavaScript running in a browser?, I would say that you need a server-side setup to connect to your redis server and then you can query your server-side as you need.
I’ve had a similar problem and my understanding from reading the above is that it caused because the browser client is trying to read the db which is on the server.
I am also using sveltekit and I resolved by problem by making sure my code was in an endpoint, which only runs on the server and then invoked the endpoint to get my data.