give an option for default host and port when building with `adapter-node`
See original GitHub issueDescribe the problem
when building an app using adapter-node
i would like to be able to choose the default port and host
right now if i run $ svelte-kit build
it automatically chooses "0.0.0.0"
as the default host and 3000
as the default port and would like to be able to control that (for the build)
the only way that i could find to change the host and port is using
$ HOST=localhost PORT=4000 node build
to start the app, but that is temporary and only affects the environmental variables and not the defaults and i would have to do that every time i start
if there is a way to specify that and i haven’t found it then i’m sorry, if this is a duplicate then i am too.
iirc there used to be a time where you could specify the --port [port]
option as you ran $ svelte-kit build
but i don’t know if that actually did anything or if i even remember correctly
Describe the proposed solution
config
i would suggest adding the option default
(or maybe defaults, given that it is port
and host
), so the config could look something like this
import node from "adapter-node"
export default {
kit: {
adapter: node({
default: {
host: "localhost",
port: 4000
}
})
}
}
implementation
i dont really know a lot about this codebase so i dont actually know how one would implement that but in the adapter-node
index.js file, lines 58 through 63, where it goes
writeFileSync(
'.svelte-kit/node/env.js',
`export const host = process.env[${JSON.stringify(
host_env
)}] || '0.0.0.0';\nexport const port = process.env[${JSON.stringify(port_env)}] || 3000;`
);
seems like an idea and then after line 37 you could add a line with
default: { host: default_host = '0.0.0.0', port: default_port = 3000 } = {},
~but i don’t know if that would work~
after a bit of testing, adding that line and expanding the first-mentioned code to
writeFileSync(
'.svelte-kit/node/env.js',
`export const host = process.env[${JSON.stringify(
host_env
)}] || ${JSON.stringify(default_host)};\nexport const port = process.env[${JSON.stringify(
port_env
)}] || ${JSON.stringify(default_port)};`
);
works as expected, a very simple fix to a very simple problem, i would just need some confirmation on whether or not this would be a good addition by the package-maintainers
Alternatives considered
like i already mentioned the only alternative i found is to start your production server with
$ HOST=localhost PORT=4000 node build
but i find that tedious and would appreciate if i could just give standard options to the builder
Importance
nice to have
Additional Information
no additional information
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:13 (2 by maintainers)
@xpat I have a similar issue. I am trying to host multiple sveltekit apps in my server. I have an added requirement that the apps run by pm2 are in specific locations. If I use
location /
, I am able to view the app in the browser. But if I use any another location in my nginx config, it doesn’t work. Do you face that issue too?This looks like is the same trap I fell into. The svelte.config.js env variables are not intended for setting server environment variable values. They tell svelte which env variable names to use. So in svelte.config.js,
The default is:
The above tells svelte to look for the env variable HOST and PORT. These must be strings and on your server in your .env or whatever you would use
HOST=localhost PORT=4000
The only time you would need to change the svelte.config.js env settings is in a case like this:
And then starting the node server like:
Basically the server determines the port, adapter-node can work on whatever port it needs as long as it knows what environment variables to look for.