allow customisation of HOST and PORT env vars in adapter-node
ADD
.changeset/curvy-eggs-sip.md
+---
+'@sveltejs/adapter-node': patch
+---
+
+Allow the environment variables containing the host and port to serve on to be customised

MODIFY
packages/adapter-node/index.d.ts
packages/adapter-node/index.d.ts
declare function plugin(options?: {
out?: string;
precompress?: boolean;
+ env?: {
+ host?: string;
+ port?: string;
+ };
}): import('@sveltejs/kit').Adapter;
export = plugin;

MODIFY
packages/adapter-node/index.js
packages/adapter-node/index.js
-import { readFileSync, statSync, createReadStream, createWriteStream } from 'fs';
+import { readFileSync, statSync, createReadStream, createWriteStream, writeFileSync } from 'fs';
import { join } from 'path';
import { fileURLToPath } from 'url';
import { pipeline } from 'stream';
@@ -12,10 +12,18 @@ const pipe = promisify(pipeline);
/**
* @param {{
* out?: string;
- * precompress?: boolean
+ * precompress?: boolean;
+ * env?: {
+ * host?: string;
+ * port?: string;
+ * };
* }} options
*/
-export default function ({ out = 'build', precompress } = {}) {
+export default function ({
+ out = 'build',
+ precompress,
+ env: { host: host_env = 'HOST', port: port_env = 'PORT' } = {}
+} = {}) {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
name: '@sveltejs/adapter-node',
@@ -34,6 +42,12 @@ export default function ({ out = 'build', precompress } = {}) {
utils.log.minor('Building server');
const files = fileURLToPath(new URL('./files', import.meta.url));
utils.copy(files, '.svelte-kit/node');
+ writeFileSync(
+ join(files, '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;`
+ );
await esbuild.build({
entryPoints: ['.svelte-kit/node/index.js'],
outfile: join(out, 'index.js'),

MODIFY
packages/adapter-node/rollup.config.js
packages/adapter-node/rollup.config.js
@@ -10,5 +10,5 @@ export default {
sourcemap: true
},
plugins: [nodeResolve(), commonjs(), json()],
- external: ['../output/server/app.js', ...require('module').builtinModules]
+ external: ['../output/server/app.js', './env.js', ...require('module').builtinModules]
};

MODIFY
packages/adapter-node/src/index.js
packages/adapter-node/src/index.js
@@ -2,11 +2,10 @@ import './require_shim';
import { createServer } from './server';
// TODO hardcoding the relative location makes this brittle
import { render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
+import { host, port } from './env.js'; // eslint-disable-line import/no-unresolved
-const { HOST = '0.0.0.0', PORT = 3000 } = process.env;
-
-const instance = createServer({ render }).listen(PORT, HOST, () => {
- console.log(`Listening on port ${PORT}`);
+const instance = createServer({ render }).listen(port, host, () => {
+ console.log(`Listening on port ${port}`);
});
export { instance };

allow customisation of HOST and PORT env vars in adapter-node (#1754)