⚠️
The vite-plugin-ssr project has been renamed Vike.
  • If you are already using vite-plugin-ssr then migrate to Vike.
  • For new projects, don't use vite-plugin-ssr but use Vike instead.

CJS

While you can use vite-plugin-ssr with CJS code, we recommend writing ESM code instead.

The code you write that is processed by Vite is already ESM. But some of your server code may not be processed by Vite and may be CJS.

If you get the following warning:

[vite-plugin-ssr][Warning] We recommend setting package.json#type to "module" (and therefore
writing ESM code instead of CJS code)

Then, in order to remove the warning, add this to your package.json:

// package.json
{
   // ...
   type: "module"
}

This makes Node.js treat all .js files as ESM.

This means that, if you have .js files written in CJS, then you'll have to migrate them to ESM. For example:

- // CJS code
- const express = require('express')
- const { renderPage } = require('vite-plugin-ssr/server')
+ // ESM code
+ import express from 'express'
+ import { renderPage } from 'vite-plugin-ssr/server'

An escape hatch is to use the .cjs and .mjs file extensions, enabling you to determine/change the module system on a file-by-file basis regardless of whether package.json#type is set to "module" or "commonjs".

You still want to use CJS? Let us know why and we'll create a new config.disableCJSWarning.