将mapbox gl geocoder添加到gatsby.js时,gatsby构建失败

将mapbox gl geocoder添加到gatsby.js时,gatsby构建失败,mapbox,gatsby,netlify,mapbox-gl,Mapbox,Gatsby,Netlify,Mapbox Gl,我有一个带有mapbox gl的gatsby.js应用程序,在我尝试包含mapbox gl geocoder包以添加搜索功能之前,该应用程序一直运行顺利 我使用npm安装了它,如下所示: Electron support has been removed The Electron net module is not consistent with the Node.js http module. See #899 for more info. npm安装mapbox/mapbox gl地理编码

我有一个带有mapbox gl的gatsby.js应用程序,在我尝试包含mapbox gl geocoder包以添加搜索功能之前,该应用程序一直运行顺利

我使用npm安装了它,如下所示:

Electron support has been removed
The Electron net module is not consistent with the Node.js http module. See #899 for more info.
npm安装mapbox/mapbox gl地理编码器——保存

然后,将其添加到反应组件中:

import '@mapbox/mapbox-gl-geocoder/lib/mapbox-gl-geocoder.css';
import MapboxGeocoder from '@mapbox/mapbox-gl-geocoder';
在useEffect挂钩内:

  map.addControl(
      new MapboxGeocoder({
        accessToken: mapboxgl.accessToken,
        zoom: 20,
        placeholder: 'Enter City',
        mapboxgl: mapboxgl
      }), 'top-left'
    );
在本地,当我运行
gatsby develope
时,它会正常工作,但当我使用Netlify部署它时,我会出现以下错误:

:24:21 PM: error Generating SSR bundle failed
1:24:21 PM: Can't resolve 'electron' in '/opt/build/repo/node_modules/@mapbox/mapbox-sdk/node_modules/got'
1:24:21 PM: If you're trying to use a package make sure that 'electron' is installed. If you're trying to use a local file make sure that the path is correct.
似乎mapbox gl geocoder依赖于@mapbox/mapbox sdk,而mapbox sdk依赖于获取某种程度上需要的数据

在Get npm页面中,我能读到的关于electron的唯一内容如下:

Electron support has been removed
The Electron net module is not consistent with the Node.js http module. See #899 for more info.
无论如何,我试着用npm安装electron,看看错误是否消失了,构建开始失败,出现了新的错误,表明窗口对象不可用

10:35:45 AM: error "window" is not available during server side rendering.

通过谷歌搜索该错误,我找到了一些关于不在
gatsby node.js中加载包的答案,但这也没有帮助。

当处理在gatsby中使用
window
的第三方模块时,您需要在其自己的网页包配置中添加
null
加载程序,以避免在SSR过程中发生传输(Sserver-SideRendering)。这是因为
gatsby develope
发生在浏览器中,而
gatsby build
发生在节点服务器中,而节点服务器显然没有

请记住,
test
值是一个正则表达式,它将匹配
node\u modules
下的文件夹,因此,请确保
/@mapbox/
是正确的名称


使用可能有效,但请记住,您正在添加另一个包,另一个捆绑文件,这可能会对性能产生影响。建议的代码段是一个内置解决方案,在您构建应用程序时会出现。

谢谢,现在可以用了!我实际上已经尝试过了,但我没有意识到它是一个正则表达式,并且插入了错误的值f或者测试!所以这只是告诉webpack“嘿,在构建过程中,不要处理那些包?”然后它们就可以不需要蒸腾了?正是你所说的。快乐的编码!