---
name: Node Environment Compatibility Guide
description: Handle multi-version Node conflicts in frontend projects, especially adapting legacy projects (Node 16) to modern tools (VitePress/Node 18+) | Node.js Environment Compatibility Guide
---

# Node Environment Compatibility Guide Skill

## Overview

This skill resolves the challenge of running legacy business projects on older Node versions such as `16.14.2` while still supporting modern tooling such as VitePress, without relying on fragile shell shortcuts.

## Adaptation Strategies

### Strategy A: NVM-Based Environment Switching (Recommended)

If project members widely use NVM, a "local version upgrade" can be achieved via `nvm exec`.

#### 1. Configure `package.json`

Do not write raw `nvm exec 18 ...` directly into `package.json`. Prefer an explicit launcher script that loads `nvm.sh` first:

```bash
#!/usr/bin/env bash
set -e
. "${NVM_DIR:-$HOME/.nvm}/nvm.sh"
exec nvm exec 18 npx vitepress "$@"
```

Then call that launcher from `package.json`:

```json
"scripts": {
  "docs:dev": "bash ./scripts/run-vitepress.sh dev docs",
  "docs:build": "bash ./scripts/run-vitepress.sh build docs"
}
```

#### 2. .nvmrc Isolation

Place a `.nvmrc` file in the `docs/` directory containing `18`.

#### 3. Boundaries

- Prefer this strategy when docs are allowed to run on a dedicated Node 18+ runtime
- If the full repository must remain on Node 16, do not force this path by default; evaluate a compatibility line instead

### Strategy A-2: Compatibility Line (Prefer for Node 16)

When the project must remain stable on Node `16.14.2`, prefer a compatibility line instead of forcing a higher docs runtime:

1. Pin a compatible docs tool version such as `vitepress@0.22.4`
2. Avoid Vite / VitePress mainlines that require Node 18+
3. Prefer `config.mjs` or `config.js`
4. Do not generate `config.ts` by default in legacy environments

### Strategy B: Directory-Level Environment Isolation (Monorepo Mode)

Treat documentation as an independent "sub-project" with its own dependency tree.

1. Create `docs/package.json` and configure standalone `engines` constraints.
2. Use a proxy script or CI/CD pipeline to specify the Node version independently.

### Strategy C: CI/CD Build Pipeline

During the build/release stage, use a dedicated Docker image or Runner node with a Node 18+ environment.

## Preflight and Guidance Logic

1. **Run `node -v`**: Get the current runtime version.
2. **Parse `package.json["engines"]`**: Evaluate hard Node version constraints.
3. **Detect version manager**:
   - `which nvm` → NVM strategy
   - `which volta` → Volta strategy
4. **Select a track**:
   - `>=18` → modern mainline
   - `16.14.2 ~ <18` → compatibility line or isolated docs runtime
   - `<16.14.2` → require a Node upgrade first
5. **Output guidance report**: Explain whether the repository should use a higher docs runtime or a pinned compatibility line.

## Report Template

```text
=== Node Compatibility Adaptation Report ===

[!] Current Environment: Node 16.14.2
[✓] Switched to the Node 16 compatibility line with a pinned VitePress version.
[✓] If an isolated docs runtime is needed, a launcher script is used instead of raw `nvm exec`.
[✓] Config now prefers config.mjs to avoid legacy CJS / TS loader conflicts.

Status: READY (compatibility adaptation applied)
```
