【Azure Developer】在使用Azure Bot Service JavaScript的实例代码遇见Cannot find module 'node:crypto' - LuBu0505/My-Code GitHub Wiki

问题描述

从Github中下载了JavaScript的Bot Service EchoBot实例代码,本地执行,总是报错 Cannot find module 'node:crypto' image.png

 错误信息

Error: Cannot find module 'node:crypto' Require stack: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15) at Function.Module._load (internal/modules/cjs/loader.js:746:27) at Module.require (internal/modules/cjs/loader.js:974:19) at require (internal/modules/cjs/helpers.js:92:18) at Object. (C:\js_echo_bot\node_modules\botframework-schema\node_modules\uuid\dist\rng.js:7:42) at Module._compile (internal/modules/cjs/loader.js:1085:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) at Module.load (internal/modules/cjs/loader.js:950:32) at Function.Module._load (internal/modules/cjs/loader.js:790:14) at Module.require (internal/modules/cjs/loader.js:974:19) { code: 'MODULE_NOT_FOUND',

问题解答

咨询了M365 Copilot后,找到了答案。就是因为本地的NodeJs版本太低的原因。 image.png

1. 检查 Node.js 版本:确保使用的 Node.js 版本支持 node:crypto 模块。

如果使用的是较旧版本的 Node.js,可能会因为不识别 node:* 语法而出现此错误。更新到 Node.js 版本 16 或更高版本应该可以解决此问题。

对于 Node.js 版本 16 及更高版本,应该使用:

const crypto = require('node:crypto');

旧版本,则需要使用:

const crypto = require('crypto');

参考资料

Error

Error: Cannot find module 'node:process'

Problem

This is caused by Node 14, which doesn't understand new syntax "node:*".

Node 14 used to have syntax:

require moduleName

However, in version 16 this was changed to:

require node:moduleName

Solution

Update to Node Version 16.

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

⚠️ **GitHub.com Fallback** ⚠️