You need to enable JavaScript to run this app. React应用Jest单元测试失败:找不到node:stream模块求助
优惠活动
大模型
产品
解决方案
定价
更多

React应用Jest单元测试失败:找不到node:stream模块求助

React应用Jest单元测试报错:找不到node:stream模块

报错信息

Test suite failed to run

Cannot find module 'node:stream' from 'index.js'

However, Jest was able to find:
    './Utils.js'
    './Utils.js.map'

You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

However, Jest was able to find:
    './utils.js'

You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

However, Jest was able to find:
    './createSerializer.js'

You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
  at Object.<anonymous> (node_modules/parse5-parser-stream/dist/cjs/index.js:4:23)

解决方案

  • 升级Jest版本:Jest 27.x及以上版本才支持Node.js的node:协议导入,直接升级Jest到最新版即可解决。执行命令:
    npm install --save-dev jest@latest
    # 或使用yarn
    yarn add --dev jest@latest
    
  • 添加模块映射(兼容旧版Jest):如果暂时无法升级Jest,在jest.config.js中配置模块映射,将node:开头的模块重定向到对应原生模块:
    module.exports = {
      // 保留原有配置
      moduleNameMapper: {
        '^node:(.*)$': '$1'
      }
    };
    
  • 检查Node.js版本:确保Node.js版本在14.13.0+或12.20.0+,过低版本不支持node:协议,升级Node.js到符合要求的版本即可。

内容的提问来源于stack exchange,提问作者Srikanth Kuriyala

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 13:20:58