import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
const cases = [];
// Case 1: node_version_and_binary_check
{
const semverParts = process.versions.node.split('.').map(Number);
const actual = {
versionFormatValid: /^v\d+\.\d+\.\d+$/.test(process.version),
major: semverParts[0],
execExists: fs.existsSync(process.execPath),
arch: process.arch,
platformSupported: ['win32', 'linux', 'darwin'].includes(process.platform),
};
const expected = {
versionFormatValid: true,
major: 24,
execExists: true,
arch: 'x64',
platformSupported: true,
};
cases.push({
name: 'node_version_and_binary_check',
passed: JSON.stringify(actual) === JSON.stringify(expected),
expected,
actual,
});
}
// Case 2: lts_vs_current_release_metadata
{
const major = Number.parseInt(process.versions.node.split('.')[0], 10);
const classifyLine = (m) => (m % 2 === 0 ? 'LTS' : 'Current');
const actual = {
releaseName: process.release.name,
ltsCodename: process.release.lts,
currentMajorLine: classifyLine(major),
v22Line: classifyLine(22),
v23Line: classifyLine(23),
};
const expected = {
releaseName: 'node',
ltsCodename: 'Krypton',
currentMajorLine: 'LTS',
v22Line: 'LTS',
v23Line: 'Current',
};
cases.push({
name: 'lts_vs_current_release_metadata',
passed: JSON.stringify(actual) === JSON.stringify(expected),
expected,
actual,
});
}
// Case 3: prebuilt_binary_layout_and_link_model
{
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nodejs-ch03-bin-'));
try {
const extractDir = path.join(tmpDir, 'node-v24.22.3-prebuilt');
const binDir = path.join(extractDir, 'bin');
const linkDir = path.join(tmpDir, 'local-bin');
fs.mkdirSync(binDir, { recursive: true });
fs.mkdirSync(linkDir, { recursive: true });
const nodeTarget = path.join(binDir, 'node');
const npmTarget = path.join(binDir, 'npm');
fs.writeFileSync(nodeTarget, '#!/bin/sh\necho v24.22.3\n', 'utf8');
fs.writeFileSync(npmTarget, '#!/bin/sh\necho 11.0.0\n', 'utf8');
const linkedBin = path.join(linkDir, 'node-bin');
fs.symlinkSync(binDir, linkedBin, process.platform === 'win32' ? 'junction' : 'dir');
const resolvedDir = fs.realpathSync(linkedBin);
const actual = {
hasNodeInBin: fs.existsSync(path.join(linkedBin, 'node')),
hasNpmInBin: fs.existsSync(path.join(linkedBin, 'npm')),
linkResolvesToBin: resolvedDir === fs.realpathSync(binDir),
};
const expected = {
hasNodeInBin: true,
hasNpmInBin: true,
linkResolvesToBin: true,
};
cases.push({
name: 'prebuilt_binary_layout_and_link_model',
passed: JSON.stringify(actual) === JSON.stringify(expected),
expected,
actual,
});
} finally {
fs.rmSync(tmpDir, { recursive: true, force: true });
}
}
// Case 4: cross_platform_install_paths
{
const winDefault = path.win32.join('C:\\Program Files\\nodejs', 'node.exe');
const posixLink = path.posix.join('/usr/local/bin', 'node');
const pathEnv = process.env.PATH || process.env.Path || '';
const pathEntries = pathEnv.split(path.delimiter).filter(Boolean);
const actual = {
winDefault,
posixLink,
delimiterMatchesOs: path.delimiter === (process.platform === 'win32' ? ';' : ':'),
hasPathEntries: pathEntries.length > 0,
};
const expected = {
winDefault: 'C:\\Program Files\\nodejs\\node.exe',
posixLink: '/usr/local/bin/node',
delimiterMatchesOs: true,
hasPathEntries: true,
};
cases.push({
name: 'cross_platform_install_paths',
passed: JSON.stringify(actual) === JSON.stringify(expected),
expected,
actual,
});
}
console.log(JSON.stringify({ version: process.version, cases }));
本例需在 Windows x64、Node.js v24.22.3 本地运行环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、Node.js v24.22.3 本地运行环境。
入口文件:projects/nodejs-install-setup-lab/explore-install-setup.mjs
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-install-setup.mjs,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、Node.js v24.22.3 本地运行环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1055.zip
课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a1041.zip