Node JS
How to use Sveltekit OG with the node adapter (@sveltejs/adapter-node)
This section details the necessary configuration to use SvelteKit OG with the Node Adapter (@sveltejs/adapter-node), targeting a standard self-hosted Node.js server environment.
Installation
First, ensure you have the Node adapter installed:
pnpm i -D @sveltejs/adapter-node
npm i -D @sveltejs/adapter-node
yarn add -D @sveltejs/adapter-node
deno add --dev npm:@sveltejs/adapter-node
In your svelte.config.js, configure the adapter:
import adapter from '@sveltejs/adapter-node';
const config = {
...,
kit: {
adapter: adapter()
},
...
};
export default config;
Plugin Configuration
You must use one of the SvelteKit OG plugins and explicitly set the esmImport option to false. This configuration ensures the Wasm module is loaded using Node's standard functions, providing reliable execution in the Node environment.
Vite Plugin (Recommended)
Warning
Vite plugin is available from sveltekit-og@v4.1.0. If you are using v4.0.0 use Rollup plugin.
If you add the plugin while the dev server is running, you might see no generated image. Stop the server and re-start it.
Add the sveltekitOG plugin to your vite.config.ts, passing { esmImport: false } to the options.
import { sveltekit } from '@sveltejs/kit/vite';
import { sveltekitOG } from '@ethercorps/sveltekit-og/plugin';
import { defineConfig } from 'vite';
const config = defineConfig({
plugins: [
sveltekit(),
sveltekitOG({
esmImport: false // Crucial for reliable Wasm loading in Node.js
})
]
});
export default config;
Rollup Plugin (Legacy)
Deprecation
If using the Rollup plugin, apply { esmImport: false } directly to the rollupWasm plugin configuration within rollupOptions.
import { sveltekit } from '@sveltejs/kit/vite';
import { rollupWasm } from '@ethercorps/sveltekit-og/plugin';
import { defineConfig } from 'vite';
const config = defineConfig({
plugins: [sveltekit()],
build: {
rollupOptions: {
plugins: [
rollupWasm({
esmImport: false // Crucial for reliable Wasm loading in Node.js
})
]
}
}
});
export default config;
Usage
Once configured, the usage remains the same as any other SvelteKit environment.
Svelte Components: Refer to the Svelte Component usage.
Raw HTML: Refer to the Raw HTML section for usage with string templates.
Preview (Self Test)
Source: https://github.com/etherCorps/sveltekit-og/tree/main/examples/node-build
To verify the Node setup works correctly, you can clone and run the dedicated example repository.
Step-by-Step Guide
- Clone the Repository and Navigate:
git clone https://github.com/etherCorps/sveltekit-og.git
cd sveltekit-og/examples/node-build
- Install Dependencies:
pnpm install
- Build the Project: This command runs the build process, applying the @sveltejs/adapter-node and the Wasm plugins, resulting in a deployable Node server build in the ./build directory.
pnpm run build
- Start the preview Server: This command starts the Node server using the generated output.
pnpm run preview
More on how to use adapter-node in sveltekit