在创建vue3+vite项目npm run dev启动项目时控制台报错TypeError: crypto.hash is not a function,原因是node版本不兼容,升到node版本22+,是正常运行的,想保持18.20.4的node版本需要一个补丁
diff --git a/dist/node/chunks/dep-Bsx9IwL8.js b/dist/node/chunks/dep-Bsx9IwL8.js
index 0583030d32a3a36aa13fbb2839435e5af6c0a9f4..43463dcb8c7a9cee2e44b29bc67902dcb360fa25 100644
--- a/dist/node/chunks/dep-Bsx9IwL8.js
+++ b/dist/node/chunks/dep-Bsx9IwL8.js
@@ -2830,8 +2830,16 @@ const singlelineCommentsRE = /\/\/.*/g;
const requestQuerySplitRE = /\?(?!.*[/|}])/;
const requestQueryMaybeEscapedSplitRE = /\\?\?(?!.*[/|}])/;
const blankReplacer = (match) => " ".repeat(match.length);
+const hash =
+ // crypto.hash is supported in Node 21.7.0+, 20.12.0+
+ crypto.hash ??
+ ((
+ algorithm,
+ data,
+ outputEncoding,
+ ) => crypto.createHash(algorithm).update(data).digest(outputEncoding))
function getHash(text, length = 8) {
- const h$2 = crypto.hash("sha256", text, "hex").substring(0, length);
+ const h$2 = hash("sha256", text, "hex").substring(0, length);
if (length <= 64) return h$2;
return h$2.padEnd(length, "_");
}