0

当我运行 gatsby build 我得到这个错误:

failed Building static HTML for pages - 10.179s

 ERROR #95313 

Building static HTML failed

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


  343 |         for (c = []; b < a; ++b) {
  344 |           for (var n = 0; n < m; ++n) {
> 345 |             c[v++] = Z(d[n] + ' ', h[b], e).trim();
      | ^
  346 |           }
  347 |         }
  348 | 


  WebpackError: The module '/node_modules/canvas/build/Release/canvas.node'
  
  - stylis.esm.js:345 
    node_modules/@emotion/stylis/dist/stylis.esm.js:345:1
  
  - stylis.esm.js:151 
    node_modules/@emotion/stylis/dist/stylis.esm.js:151:1
  
  - stylis.esm.js:175 
    node_modules/@emotion/stylis/dist/stylis.esm.js:175:1
  
  - stylis.esm.js:286 
    node_modules/@emotion/stylis/dist/stylis.esm.js:286:1
  
  - stylis.esm.js:151 
    node_modules/@emotion/stylis/dist/stylis.esm.js:151:1
  
  - stylis.esm.js:175 
    node_modules/@emotion/stylis/dist/stylis.esm.js:175:1
  
  - stylis.esm.js:286 
    node_modules/@emotion/stylis/dist/stylis.esm.js:286:1
  
  - stylis.esm.js:151 

怎么解决?运行 gatsby develop 时没有错误。

4

2 回答 2

0

将此代码段添加到gatsby-node.js

exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
  if (stage === "build-html") {
    actions.setWebpackConfig({
      module: {
          rules: [
          {
            test: /canvas/,
            use: loaders.null(),
          },
         ],
      },
    })
  } 
}

有一个包试图访问全局对象,例如windowdocument在您的 SSR(服务器渲染中,显然没有定义(它甚至存在),因为gatsby-build发生在节点服务器中,而gatsby develop发生在浏览器端,其中存在window和编译时间。null使用上面的代码片段,当 webpack 转译代码时,您正在向有问题的模块添加一个加载器。

规则测试是一个正则表达式(因此是大括号,/),它与里面的文件夹名称匹配node_modules。输出错误表明存在canvas问题,但您可能需要将其更改为/stylis/

于 2020-11-02T14:00:23.917 回答
0

更新 gatsby-config.js 以包含插件 gatsby-plugin-emotion:

module.exports = {
  plugins: [
    `gatsby-plugin-emotion`,
  ],
}

这需要重新启动 gatsby 开发过程。

于 2020-11-02T13:26:29.960 回答