question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

give an option for default host and port when building with `adapter-node`

See original GitHub issue

Describe 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sridharramancommented, Apr 15, 2022

@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?

1reaction
cbergencommented, Jan 5, 2022

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:

// svelte.config.js
import adapter from '@sveltejs/adapter-node';

export default {
	kit: {
		adapter: adapter({
			// default options are shown
			out: 'build',
			precompress: false,
			env: {
				host: 'HOST',
				port: 'PORT'
			}
		})
	}
};

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:

// svelte.config.js
import adapter from '@sveltejs/adapter-node';

export default {
	kit: {
		adapter: adapter({
			out: 'build',
			precompress: false,
			env: {
				host: 'MYHOST',
				port: 'MYPORT'
			}
		})
	}
};

And then starting the node server like:

MYHOST=localhost MYPORT=4000 node build

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

unexpected behavior when run `npm run dev` after set config ...
... adapter: node({ // default options are shown out: 'build', precompress: false, env: { host: 'HOST', port: 'PORT' } }), paths: { assets: ......
Read more >
@sveltejs/adapter-node - npm
Environment variables. PORT and HOST. By default, the server will accept connections on 0.0.0.0 using port 3000.
Read more >
sveltekit build port is always 3000 but it is not available in my ...
After "npm run build", you can change the default port 3000 in the build\index.js file(Line 218). const port = env('PORT', !path && '3000');....
Read more >
Adapters • Docs • SvelteKit
You can pass options to adapters, such as customising the output directory: svelte.config.js import adapter from '@sveltejs/adapter-node'; export default ...
Read more >
@astrojs/node Astro Documentation
standalone mode builds to server that automatically starts with the entry module is run. This allows you to more easily deploy your build...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found