Vike Server Integration (Deprecated)
The `vike-server` package historically provided utilities for integrating Vike with various Node.js server frameworks like Express.js or Hono, enabling server-side rendering (SSR), hot module replacement (HMR) for server code, and zero-configuration setups with Vite. It aimed to transpiles server code with Vite, eliminating the need for separate tools like `ts-node`. However, `vike-server` (current version 1.0.25) is now deprecated. Vike applications should instead leverage the built-in `+server.js` convention for flexible server integration or migrate to `vike-photon` for advanced edge and serverless deployments. Vike itself, the meta-framework that `vike-server` extended, is currently in a stable 1.x.x release series, focusing on a robust, unopinionated foundation for building web applications with SSR, routing, and data loading atop Vite, and is known for its stable core and gradual evolution.
Common errors
-
Error: Cannot find module 'vike-server/express' from 'your-server-file.js'
cause The `vike-server` package (or its framework-specific entry point) is either not installed, or you are attempting to use it after having migrated to `vike-photon` or `+server.js` and removed it.fixVerify `vike-server` is listed in your `package.json` and installed. If you intend to use `vike-photon` or `+server.js`, ensure `vike-server` is uninstalled and your server entry point is updated according to the Vike migration guides for `+server` or `vike-photon`. -
TypeError: Cannot read properties of undefined (reading 'pageContext')
cause This error often indicates an issue with Vike's server-side rendering setup, where `pageContext` is not properly populated or passed. It could be due to an incomplete `vike-server` setup, or a misconfiguration after migrating away from it.fixEnsure `vike-server` (or its replacement `vike-photon`/`+server.js`) is correctly initialized and applied to your server framework. Check Vike's documentation on `pageContext` and server integration for the correct setup, especially if you recently upgraded Vike or are migrating away from `vike-server`.
Warnings
- breaking `vike-server` is officially deprecated. Modern Vike applications should migrate to using the `+server.js` convention for server-side routing and API endpoints, or utilize the `vike-photon` package for robust edge deployments. Continued use of `vike-server` is discouraged and may lead to compatibility issues with newer Vike and Vite versions.
- breaking Older versions of Vike's server integration (prior to `vike-server` and `vike-photon`) used `vike-node/connect`, which was replaced by `vike-node/express`. If migrating from a very old setup, this change is relevant.
- gotcha Vike introduced 'plus files' (`+Page.js`, `+server.js`, `+onBeforeRender.js`) as the recommended convention for defining pages and server-side logic, replacing the older `.page.js` suffixes. While `vike-server` itself didn't directly control this, the deprecation aligns with Vike's overall move towards 'plus files' and away from implicit server integration via `vike-server`.
- breaking Recent versions of Vike (and by extension its server tooling) require Node.js 20 or above. Using older Node.js versions may lead to runtime errors or compatibility issues.
Install
-
npm install vike-server -
yarn add vike-server -
pnpm add vike-server
Imports
- apply
const { apply } = require('vike-server/express')import { apply } from 'vike-server/express' - serve
import { serve } from 'vike-server/express'import { serve } from 'vike-server/express/serve' - * as ExpressVikeServer
import * as ExpressVikeServer from 'vike-server/express'
Quickstart
import express from 'express';
import { apply, serve } from 'vike-server/express'; // DEPRECATED - see warnings
async function startServer() {
const app = express();
// Apply Vike middleware to Express
// This would integrate Vite's dev server in dev mode
// and serve Vike's production build in production.
await apply(app);
// Start the server
const port = process.env.PORT ? parseInt(process.env.PORT) : 3000;
const server = await serve(app, { port });
console.log(`Server running at http://localhost:${port}`);
console.warn('WARNING: vike-server is deprecated. Migrate to +server.js or vike-photon.');
return server;
}
startServer();
// Example Vike page configuration (for context, not part of vike-server)
// pages/index/+Page.js
/*
export { Page }
function Page() {
return <h1>Hello, Vike!</h1>
}
*/
// Example vite.config.js (for context)
/*
import react from '@vitejs/plugin-react'
import vike from 'vike/plugin'
export default {
plugins: [react(), vike()],
};
*/