Skip to main content

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
	

In your svelte.config.js, configure the adapter:

svelte.config.js
		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.

Add the sveltekitOG plugin to your vite.config.ts, passing { esmImport: false } to the options.

vite.config.ts
		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)

If using the Rollup plugin, apply { esmImport: false } directly to the rollupWasm plugin configuration within rollupOptions.

vite.config.ts
		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.

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:
Bash
		git clone https://github.com/etherCorps/sveltekit-og.git
cd sveltekit-og/examples/node-build
	
  • Install Dependencies:
Bash
		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.
Bash
		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