Node.js v22 到 v24
AugustinMauroy, Richard Lau
Node.js:从 v22 到 v24
本文涵盖从 Node.js v22 到 v24 迁移的部分内容。用户态迁移团队正在开发更多 codemod,以帮助你顺利完成此次 迁移。
缺失的 codemod 会随着时间的推移逐步补充。如果你有特定需求。
请阅读此 issue,了解该流程目前进行到哪一步,以及如何手动迁移你的代码。
随着 Node.js 24.11.0 的发布,Node.js 24 发布线已进入 Long-Term Support (LTS) 阶段,并将持续获得更新,直至 2028 年 4 月底。
如果你正在从 Node.js 22 LTS 迁移,以下内容概述了 Node.js 23.0.0 和 Node.js 24.0.0 中引入的破坏性变更。
平台支持
Node.js 不再为以下平台提供 预构建二进制文件:
- 32 位 Windows (x86),自 Node.js 23.0.0 起。
- armv7 上的 32 位 Linux,自 Node.js 24.0.0 起。
macOS 的预构建二进制文件现在要求最低 macOS 13.5。
arm64、ppc64le、s390x 和 x64 上 Linux 的预构建二进制文件继续兼容 glibc 2.28 及以上版本 (与 Node.js 22 相比无变化)。
如果你从源码构建 Node.js,请参阅 其他说明。
破坏性变更
OpenSSL 3.5
Node.js 24 LTS 的预构建二进制文件,或使用默认构建配置选项的构建版本,均包含 OpenSSL 3.5。Node.js 24 LTS 使用 OpenSSL 3.5 的默认安全级别 2,这意味着:
- 禁止使用长度小于 2048 位的 RSA、DSA 和 DH 密钥,以及长度小于 224 位的 ECC 密钥。
- 任何使用 RC4 的密码套件也都被禁止。
如果你依赖较旧的密钥或弱加密算法,请在升级前基于 Node.js 24 构建版本测试你的工作负载 (或调整密钥/加密算法选择)。
其他行为变更与参数校验
此版本包含多项行为和校验变更 (更严格的 fetch() 合规要求、AbortSignal 校验、stream/pipe 错误现在会抛出、Buffer 行为变化、Windows 上的路径处理修复、test runner 默认设置变更等)。
C/C++ 插件
链接到 V8 API 的插件可能需要针对 V8 13.6 进行更新;在以前使用 C++17 的情况下,可能需要 C++20 支持。在可能的情况下,优先使用 NODE-API 以减少重复重建。
从源代码构建 Node.js
如果你是从源代码构建 Node.js,可能需要更新编译器工具链:
Node.js 的 configure 脚本会在你尝试使用不满足最低支持版本要求的编译器工具链构建 Node.js 时发出警告,但不会主动阻止你进行尝试。
可用的 Codemod
Node.js 23 和 24 中的一些破坏性变更或生命周期结束(EOL)弃用配有相关的 Codemod,可帮助你更新代码库。以下是此次迁移可用的 Codemod 列表:
crypto-rsa-pss-update
在 DEP0154 中,crypto 模块中的 generateKeyPair 和 generateKeyPairSync 方法弃用了 'rsa-pss' 密钥类型的 hash、mgf1Hash 和 saltLength 选项,分别改用 hashAlgorithm、mgf1HashAlgorithm 和 saltLength。
此 Codemod 的源代码可在 crypto-rsa-pss-update directory 中找到。
你可以在 Codemod Registry 中找到此 Codemod。
npx codemod run @nodejs/crypto-rsa-pss-update示例
const crypto = require("node:crypto");
crypto.generateKeyPair(
"rsa-pss",
{
modulusLength: 2048,
- hash: "sha256",
- mgf1Hash: "sha1",
+ hashAlgorithm: "sha256",
+ mgf1HashAlgorithm: "sha1",
saltLength: 32,
},
(err, publicKey, privateKey) => {
// callback
},
);
crypto.generateKeyPairSync("rsa-pss", {
modulusLength: 2048,
- hash: "sha256",
+ hashAlgorithm: "sha256",
});dirent-path-to-parent-path
此 Codemod 会将 dirent.path 的用法转换为使用 dirent.parentPath。
请参阅 DEP0178。
此 Codemod 的源代码可在 dirent-path-to-parent-path directory 中找到。
你可以在 Codemod Registry 中找到此 Codemod。
npx codemod run @nodejs/dirent-path-to-parent-path示例
readdir
const { readdir } = require('node:fs/promises');
const entries = await readdir('/some/path', { withFileTypes: true });
for (const dirent of entries) {
- console.log(dirent.path);
+ console.log(dirent.parentPath);
}opendir
import { opendir } from 'node:fs/promises';
const dir = await opendir('./');
for await (const dirent of dir) {
- console.log(`Found ${dirent.name} in ${dirent.path}`);
+ console.log(`Found ${dirent.name} in ${dirent.parentPath}`);
}fs-access-mode-constants
fs 模块对直接暴露在 node:fs 上的 F_OK、R_OK、W_OK 和 X_OK getter 引入了运行时弃用。应改为从 fs.constants 或 fs.promises.constants 获取它们。
此 Codemod 处理 DEP0176。
此 Codemod 的源代码可在 fs-access-mode-constants directory 中找到。
你可以在 Codemod Registry 中找到此 Codemod。
npx codemod run @nodejs/fs-access-mode-constants示例
const fs = require('node:fs');
- fs.access('/path/to/file', fs.F_OK, callback);
+ fs.access('/path/to/file', fs.constants.F_OK, callback);
- fs.access('/path/to/file', fs.R_OK | fs.W_OK, callback);
+ fs.access('/path/to/file', fs.constants.R_OK | fs.constants.W_OK, callback);fs-truncate-fd-deprecation
fs.truncate 函数在与文件描述符配合使用时已被弃用(DEP0081)。请改用 fs.ftruncate。
此 codemod 的源代码可在 fs-truncate-fd-deprecation 目录 中找到。
你可以在 Codemod Registry 中找到此 codemod。
npx codemod run @nodejs/fs-truncate-fd-deprecation示例
- const { truncate, open, close } = require('node:fs');
+ const { ftruncate, open, close } = require('node:fs');
open('file.txt', 'w', (err, fd) => {
if (err) throw err;
- truncate(fd, 10, (err) => {
+ ftruncate(fd, 10, (err) => {
if (err) throw err;
close(fd, () => {});
});
});http2-priority-signaling
由于优先级信令已被弃用,此方案会移除与 HTTP/2 优先级相关的选项和方法。
请参阅 DEP0194。
此 codemod 的源代码可在 http2-priority-signaling 目录 中找到。
你可以在 Codemod Registry 中找到此 codemod。
npx codemod run @nodejs/http2-priority-signaling此 codemod 的作用
- 从
http2.connect()调用选项中移除priority属性 - 从
session.request()调用选项中移除priority属性 - 移除整个
stream.priority()方法调用语句 - 从
client.settings()调用选项中移除priority属性 - 同时处理 CommonJS (
require()) 和 ESM (import) 导入
示例
import http2 from "node:http2";
const session = http2.connect("https://example.com", {
- priority: { weight: 16, parent: 0, exclusive: false }
});
const stream = session.request({
":path": "/api/data",
- priority: { weight: 32 }
});
- stream.priority({ exclusive: true, parent: 0, weight: 128 });
const client = http2.connect("https://example.com");
-client.settings({ enablePush: false, priority: true });
+client.settings({ enablePush: false });process-assert-to-node-assert
在 Node.js v23 中,DEP0100 弃用已进入生命周期终止(End-of-Life)阶段,process.assert 已被移除,转而推荐直接使用 assert 模块。此 codemod 将帮助你将所有对 process.assert 的引用替换为相应的 assert 模块导入和 assert() 用法。
此 codemod 的源代码可在 process-assert-to-node-assert 目录 中找到。
你可以在 Codemod Registry 中找到此 codemod。
npx codemod run @nodejs/process-assert-to-node-assert示例
+ import assert from "node:assert";
- process.assert(condition, "Assertion failed");
+ assert(condition, "Assertion failed");补充说明
此 codemod 使用 fs 功能 来读取 package.json 文件,并判断项目使用的是 ES modules 还是 CommonJS。基于这些信息,它会为 assert 模块添加相应的导入语句。
tls-create-secure-pair-to-tls-socket
此方案将用法从已弃用的 createSecurePair() 转换为 TLSSocket()。
请参阅 DEP0064。
此 codemod 的源代码可在 tls-create-secure-pair-to-tls-socket 目录 中找到。
你可以在 Codemod Registry 中找到此 codemod。
npx codemod run @nodejs/tls-create-secure-pair-to-tls-socket示例
-const { createSecurePair } = require('node:tls');
+const { TLSSocket } = require('node:tls');
-const pair = createSecurePair(credentials, true, true, false);
+const socket = new TLSSocket(underlyingSocket, {
+ secureContext: credentials,
+ isServer: true,
+ requestCert: true,
+ rejectUnauthorized: false
+});