I have a Sveltekit app which must register itself through our backend by sending a request containing it's name and the port of the webserver. I'm trying to move this logic away from build-time file copying into hooks.server.ts
, but am having trouble figuring out how to obtain the dynamically bound port. Is there a way to get it?
Asked
Active
Viewed 15 times
0

kalkronline
- 459
- 2
- 12
-
[Environment variables](https://kit.svelte.dev/docs/modules#$env-dynamic-private) maybe? – H.B. Aug 16 '23 at 21:49
-
@H.B. that wouldn't work because the port is dynamically assigned. – kalkronline Aug 16 '23 at 23:30
-
Where is the port coming from if it is not configured or passed in via env. variables? Why is it dynamic? – H.B. Aug 16 '23 at 23:46
1 Answers
0
See Environment Variables or PORT, Host, and Socket Path section in the SvelteKit docs. From those docs:
In production, .env files are not automatically loaded. To do so, install dotenv in your project...
npm install dotenv
...and invoke it before running the built app:
node -r dotenv/config build
Or you can configure it manually with
HOST=127.0.0.1 PORT=4000 node build

Jason Holtzen
- 183
- 2
- 10