简体   繁体   English

Gatsby:WebpackError [React Intl] 找不到所需的 `intl` 对象。<IntlProvider> 需要存在于组件祖先中

[英]Gatsby: WebpackError [React Intl] Could not find required `intl` object. <IntlProvider> needs to exist in the component ancestry

I'm using React Intl for internationalisation in my Gatsby app .我在Gatsby 应用程序中使用React Intl进行国际化。 The app runs fine as expected with npm start , but the build fails with an error during execution of npm run build command.该应用程序在使用npm start按预期运行良好,但在执行npm run build命令期间构建失败并出现错误。

Code代码

// gatsby-browser.js

import React from "react"
import { IntlProvider } from "react-intl"

export const wrapRootElement = ({ element }) => (
  <IntlProvider locale="en" messages={{ en: {} }}>
    {element}
  </IntlProvider>
)
// src/pages/index.js

import React from "react"
import { useIntl } from "react-intl"

const IndexPage = () => {
  const intl = useIntl()

  return <p>index.js</p>
}

export default 

Error错误

The npm run build command fails with this error. npm run build命令失败并显示此错误。

failed Building static HTML for pages - 0.193s

 ERROR #95313 

Building static HTML failed for path "/"

See our docs page for more info on this error: https://gatsby.dev/debug-html


  74 |     if (Err === void 0) { Err = Error; }
  75 |     if (!condition) {
> 76 |         throw new Err(message);
     | ^
  77 |     }
  78 | }
  79 |


  WebpackError: [React Intl] Could not find required `intl` object. <IntlProvider> needs to exist in the component ancestry.
  
  - utils.js:76 
    [gatsby-starter-default]/[@formatjs]/ecma402-abstract/lib/utils.js:76:1
  
  - utils.js:6 
    [gatsby-starter-default]/[react-intl]/lib/src/utils.js:6:14
  
  - useIntl.js:6 
    [gatsby-starter-default]/[react-intl]/lib/src/components/useIntl.js:6:25
  
  - index.js:5 
    gatsby-starter-default/src/pages/index.js:5:23
  
  - extends.js:3 
    [gatsby-starter-default]/[@babel]/runtime/helpers/extends.js:3:42
  
  - extends.js:2 
    [gatsby-starter-default]/[@babel]/runtime/helpers/extends.js:2:1
  
  - extends.js:13 
    [gatsby-starter-default]/[@babel]/runtime/helpers/extends.js:13:1
  
  - static-entry.js:286 
    gatsby-starter-default/.cache/static-entry.js:286:22
  
  - index.js:72 
    [gatsby-starter-default]/[@formatjs]/fast-memoize/lib/index.js:72:1
  

not finished Caching JavaScript and CSS webpack compilation - 6.623s
not finished Caching HTML renderer compilation - 0.243s

Gatsby Documentation Says盖茨比文档说

The APIs wrapPageElement and wrapRootElement exist in both the browser and SSR APIs . API wrapPageElementwrapRootElement存在于浏览器SSR API 中 You generally should implement the same components in both gatsby-ssr.js and gatsby-browser.js so that pages generated through SSR with Node.js are the same after being hydrated in the browser.你通常应该在gatsby-ssr.jsgatsby-browser.js实现相同的组件,以便通过 SSR 和 Node.js 生成的页面在浏览器中水化后是相同的。

Solution解决方案

As document suggests to implement wrapRootElement in both gatsby-ssr.js and gatsby-browser.js , I added the code in gatsby-ssr.js .正如文档建议在gatsby-ssr.jsgatsby-browser.js实现wrapRootElement ,我在gatsby-ssr.js添加了代码。 It worked like charm and the build didn't fail.它像魅力一样工作,并且构建没有失败。

For an unknown reason, the IntlProvider context was getting lost during build time in SSR .由于未知原因, IntlProvider上下文SSR 的构建期间丢失了。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM