Node adapter deployment helps you run your Svelte app on a Node.js server easily. It turns your app into code that Node.js understands.
Node adapter deployment in Svelte
import adapter from '@sveltejs/adapter-node'; export default { kit: { adapter: adapter({ out: 'build', // folder for built files precompress: false // optional gzip compression }) } };
The adapter-node package must be installed first with npm install -D @sveltejs/adapter-node.
The out option sets where the build files go. Default is build.
import adapter from '@sveltejs/adapter-node'; export default { kit: { adapter: adapter() } };
import adapter from '@sveltejs/adapter-node'; export default { kit: { adapter: adapter({ out: 'my-server', precompress: true }) } };
This config tells SvelteKit to build the app for Node.js deployment. After building, you can run the app with Node.js from the build folder.
import adapter from '@sveltejs/adapter-node'; export default { kit: { adapter: adapter({ out: 'build' }) } };
Remember to install the adapter package before using it.
After building, use node build/index.js to start your app server.
Node adapter supports server-side rendering and API routes.
Node adapter lets you run Svelte apps on Node.js servers.
Configure it in svelte.config.js with @sveltejs/adapter-node.
Build your app, then start it with Node.js from the output folder.
Practice
@sveltejs/adapter-node in a Svelte project?Solution
Step 1: Understand adapter purpose
The@sveltejs/adapter-nodeis designed to prepare Svelte apps to run on Node.js servers.Step 2: Compare options
Options describing conversion to React, browser-only support, or automatic CSS styling are unrelated. Only deploying the Svelte app on a Node.js server correctly states the adapter's purpose.Final Answer:
To deploy the Svelte app on a Node.js server -> Option BQuick Check:
Node adapter = deploy on Node.js server [OK]
- Thinking adapter converts Svelte to React
- Assuming adapter only works in browsers
- Confusing adapter with styling tools
svelte.config.js?Solution
Step 1: Check correct adapter usage
The Node adapter must be imported and called as a function inside thekit.adapterproperty.Step 2: Validate syntax
import adapter from '@sveltejs/adapter-node'; export default { kit: { adapter: adapter() } };correctly usesadapter: adapter(). Incorrect configurations have invalid object syntax like{ adapter() }, fail to calladapteras a function, or place it outsidekit.Final Answer:
import adapter from '@sveltejs/adapter-node'; export default { kit: { adapter: adapter() } }; -> Option AQuick Check:
Call adapter() inside kit.adapter [OK]
- Forgetting to call adapter as a function
- Placing adapter outside the kit object
- Using adapter without parentheses
npm run build node build/index.js
What will happen when you run
node build/index.js?Solution
Step 1: Understand build output
Using the Node adapter,npm run buildcreates a Node.js server app in thebuildfolder, includingindex.js.Step 2: Running the server
Runningnode build/index.jsstarts the Node.js server that serves the Svelte app.Final Answer:
The Svelte app starts a Node.js server serving the built app -> Option DQuick Check:
Build + node index.js = start server [OK]
- Thinking index.js is missing after build
- Expecting app to open in browser automatically
- Confusing build folder with source code
@sveltejs/adapter-node in svelte.config.js but running node build/index.js gives an error: Cannot find module 'devalue'. What is the likely cause?Solution
Step 1: Understand the error
The error means Node.js cannot find thedevaluepackage, which the adapter-node server depends on.Step 2: Check dependencies
This usually happens ifnpm installwas not run to install all required packages.Final Answer:
You forgot to install dependencies with npm install -> Option AQuick Check:
Missing module error = run npm install [OK]
- Assuming adapter import error causes missing module
- Confusing dev and build commands
- Ignoring dependency installation step
Solution
Step 1: Understand Node adapter port config
The Node adapter server reads the port from the environment variablePORTby default.Step 2: Set environment variable before start
Settingprocess.env.PORT = 4000or runningPORT=4000 node build/index.jssets the port correctly.Final Answer:
Set process.env.PORT = 4000 before running node build/index.js -> Option CQuick Check:
Port set via environment variable PORT [OK]
- Trying to set port in svelte.config.js (not supported)
- Modifying build/index.js directly (not recommended)
- Adding port flags in package.json scripts without env
Ready to prove it?
Pass the test and this pattern moves unknown
