简体   繁体   English

Jest失败并出现错误:无法找到模块'react / lib / ReactComponentTreeHook'

[英]Jest fails with error: Cannot find module 'react/lib/ReactComponentTreeHook'

I have installed Jest v17.0.3 in my react project. 我在我的react项目中安装了Jest v17.0.3。

When I run jest locally it works fine, but on the build server it fails with: 当我在本地运行jest它工作正常,但在构建服务器上它失败了:

Error: Cannot find module 'react/lib/ReactComponentTreeHook' from 'ReactDebugTool.js' 错误:无法从'ReactDebugTool.js'中找到模块'react / lib / ReactComponentTreeHook'

Both machines are running node version 6.9.1 and npm version 4.0.2. 两台机器都运行节点版本6.9.1和npm版本4.0.2。

use same version of react and react-dom. 使用相同版本的react和react-dom。 My problem fixed after using this command 使用此命令后我的问题已修复

npm install --save react@15.4.0 react-dom@15.4.0

this problem specially occurs on react 15.4.0 above. 这个问题特别发生在上面的15.4.0反应中。

Can you check which version of React you are using? 你能查看你正在使用哪个版本的React吗? Is it the same on both servers? 两台服务器上都一样吗? I would try removing node_modules and reinstalling the dependencies. 我会尝试删除node_modules并重新安装依赖项。 The reason I am suggesting this is that in React v15.4.0 you cannot import private apis and it seems that ReactDebugTools.js is trying to import from react/lib/... . 我建议这样做的原因是在React v15.4.0中你无法导入私有api,似乎ReactDebugTools.js试图从react/lib/...导入。

From the blogpost about React v15.4.0 ( Link ): 来自关于React v15.4.0( 链接 )的博文:

However, there is a possibility that you imported private APIs from react/lib/*, or that a package you rely on might use them. 但是,您可能从react / lib / *导入了私有API,或者您依赖的包可能会使用它们。 We would like to remind you that this was never supported, and that your apps should not rely on internal APIs. 我们想提醒您,这从未受到支持,您的应用不应依赖内部API。 The React internals will keep changing as we work to make React better. 当我们努力使React更好时,React内部结构将不断变化。

Hope this helps! 希望这可以帮助!

In the latest versions of react we often see this error as we have loaded 2 versions of react: 在最新版本的反应中,我们经常看到这个错误,因为我们加载了2个版本的反应:

To make sure you have just 1 version, run the following in your terminal: 要确保您只有1个版本,请在终端中运行以下命令:

  1. npm ls react-dom npm ls react-dom 在此输入图像描述

  2. npm ls react npm ls反应 在此输入图像描述

Both the react and react-dom versions need to be same. react和react-dom版本都需要相同。 If any one of these returns more than 1 version then that's not supported. 如果其中任何一个返回超过1个版本,则不支持。 You have to then correct it in your corresponding package.json 您必须在相应的package.json中更正它

我有同样的问题,我删除了node_modules并运行了npm install,它修复了问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 迁移到React 16.0.0错误:找不到模块“ react / lib / ReactComponentTreeHook” - Migration to React 16.0.0 Error: Cannot find module “react/lib/ReactComponentTreeHook” 无法从“ReactDebugTool.js”中找到模块“react/lib/ReactComponentTreeHook” - Cannot find module 'react/lib/ReactComponentTreeHook' from 'ReactDebugTool.js' 更新到React 16.2.0时找不到模块“ react / lib / ReactComponentTreeHook”错误 - cannot find module “react/lib/ReactComponentTreeHook” error when updating to React 16.2.0 玩笑失败 React 16 升级后无法从“ReactShallowRenderer.js”中找到模块“react/lib/React” - Jest fails Cannot find module 'react/lib/React' from 'ReactShallowRenderer.js' after React 16 upgrade 无法解析模块`react / lib / ReactComponentTreeHook` - Unable to resolve module `react/lib/ReactComponentTreeHook` ReactJS:找不到模块:无法解析'react / lib / ReactComponentTreeHook' - ReactJS: Module not found: Can't resolve 'react/lib/ReactComponentTreeHook' 错误:找不到模块“ react / lib / invariant” - Error: Cannot find module 'react/lib/invariant' 用酶和玩笑测试 React 给出错误无法从“index.js”中找到模块“@babel/polyfill/lib/noConflict” - Testing React with enzyme and jest giving error Cannot find module '@babel/polyfill/lib/noConflict' from 'index.js' 设置Jest和Enzyme以测试React 15无法找到模块react / lib / ReactTestUtils - Setting up Jest and Enzyme to test React 15 cannot find module react/lib/ReactTestUtils REACT JEST错误无法从'jest-expect.js'中找到模块'jest-matchers' - REACT JEST error Cannot find module 'jest-matchers' from 'jest-expect.js'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM