蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 Node.js v24 版本信息、脚本模式与异步非阻塞输出顺序

JAVASCRIPTWindows x64、Node.js v24.22.3 本地运行环境。explore-tutorial.mjs
explore-tutorial.mjs106 行
import { spawnSync } from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';

const cases = [];

// Case 1: version_and_runtime_info
{
  const major = Number.parseInt(process.version.slice(1).split('.')[0], 10);
  const actual = {
    startsWithV: process.version.startsWith('v'),
    majorVersion: major,
    hasV8: typeof process.versions.v8 === 'string' && process.versions.v8.length > 0,
    hasLibuv: typeof process.versions.uv === 'string' && process.versions.uv.length > 0,
    crossPlatformSupported: ['win32', 'linux', 'darwin'].includes(process.platform),
  };
  const expected = {
    startsWithV: true,
    majorVersion: 24,
    hasV8: true,
    hasLibuv: true,
    crossPlatformSupported: true,
  };
  cases.push({
    name: 'version_and_runtime_info',
    passed: JSON.stringify(actual) === JSON.stringify(expected),
    expected,
    actual,
  });
}

// Case 2: script_mode_hello_world
{
  const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nodejs-ch01-'));
  try {
    const scriptPath = path.join(tmpDir, 'helloworld.js');
    fs.writeFileSync(scriptPath, 'console.log("Hello World");\n', 'utf8');
    const run = spawnSync(process.execPath, [scriptPath], { encoding: 'utf8' });
    const actual = {
      exitCode: run.status,
      stdout: run.stdout.trim(),
    };
    const expected = {
      exitCode: 0,
      stdout: 'Hello World',
    };
    cases.push({
      name: 'script_mode_hello_world',
      passed: JSON.stringify(actual) === JSON.stringify(expected),
      expected,
      actual,
    });
  } finally {
    fs.rmSync(tmpDir, { recursive: true, force: true });
  }
}

// Case 3: interactive_eval_hello
{
  const run = spawnSync(process.execPath, ['-e', "console.log('Hello World!');"], {
    encoding: 'utf8',
  });
  const actual = {
    exitCode: run.status,
    stdout: run.stdout.trim(),
  };
  const expected = {
    exitCode: 0,
    stdout: 'Hello World!',
  };
  cases.push({
    name: 'interactive_eval_hello',
    passed: JSON.stringify(actual) === JSON.stringify(expected),
    expected,
    actual,
  });
}

// Case 4: non_blocking_async_order
{
  const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nodejs-ch01-io-'));
  try {
    const sampleFile = path.join(tmpDir, 'note.txt');
    fs.writeFileSync(sampleFile, 'event-driven-io', 'utf8');
    const order = [];
    const readPromise = fs.promises.readFile(sampleFile, 'utf8').then((text) => {
      order.push(`async:${text}`);
    });
    order.push('sync:continue');
    await readPromise;
    const actual = { order };
    const expected = { order: ['sync:continue', 'async:event-driven-io'] };
    cases.push({
      name: 'non_blocking_async_order',
      passed: JSON.stringify(actual) === JSON.stringify(expected),
      expected,
      actual,
    });
  } finally {
    fs.rmSync(tmpDir, { recursive: true, force: true });
  }
}

console.log(JSON.stringify({ version: process.version, cases }));
本例需在 Windows x64、Node.js v24.22.3 本地运行环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、Node.js v24.22.3 本地运行环境。

入口文件:projects/nodejs-tutorial-lab/explore-tutorial.mjs

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-tutorial.mjs,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、Node.js v24.22.3 本地运行环境。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Node.js

课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a1041.zip

下载课程关联工程包:Node.js

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1073.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习