Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/vite/src/node/__tests__/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, test } from 'vitest'
import { checkNodeVersion } from '../cli'

describe('CLI Node.js version checking', () => {
test.each([
// Unsupported versions
['18.20.0', false],
['20.18.5', false],
['22.11.0', false],
// Supported versions
['20.19.0', true],
['20.20.1', true],
['22.12.0', true],
['22.13.1', true],
['23.0.0', true],
])('should return %p for Node.js version %s', (version, expected) => {
const result = checkNodeVersion(version)
expect(result).toBe(expected)
})
})
31 changes: 31 additions & 0 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,37 @@ import { createLogger } from './logger'
import { resolveConfig } from './config'
import type { InlineConfig } from './config'

/**
* Check if the current Node.js version is supported
* @param nodeVersion - The Node.js version string (e.g., '20.19.0')
* @returns true if the version is supported, false otherwise
*/
export function checkNodeVersion(nodeVersion: string): boolean {
const currentVersion = nodeVersion.split('.')
const major = parseInt(currentVersion[0], 10)
const minor = parseInt(currentVersion[1], 10)
const patch = parseInt(currentVersion[2], 10)

const isSupported =
(major === 20 && (minor > 19 || (minor === 19 && patch >= 0))) ||
(major === 22 && minor >= 12) ||
major > 22

return isSupported
}

// Check Node.js version before proceeding
if (!checkNodeVersion(process.versions.node)) {
// eslint-disable-next-line no-console
console.warn(
colors.yellow(
`You are using Node.js ${process.versions.node}. ` +
`Vite requires Node.js version 20.19+ or 22.12+. ` +
`Please upgrade your Node.js version.`,
),
)
}

const cli = cac('vite')

// global options
Expand Down
Loading