11

我正在创建一个个人网站/投资组合,我偶然发现了强大的 gatsby.js 包。

为了研究目的,我还必须可视化一个复杂的数据集,并且我想使用 d3.js,并将我在 Gatsby 支持的站点中创建的仪表板包括在内。

可以在反应组件中使用 d3 -> https://medium.com/@Elijah_Meeks/interactive-applications-with-react-d3-f76f7b3ebc71

从理论上讲,Gatsby 应该能够支持 d3 集成,但我的尝试到目前为止都失败了。

这是我尝试过的:

完成 Gatsby 教程https://www.gatsbyjs.org/tutorial/

我正在使用 gatsbyjs 文档中已完成的第 4 个教程站点,并添加了以下内容

npm install --save d3

添加了 utils/d3.js

文件内容

import d3 from "d3"
module.exports = d3

我还在 gatsby-config.js 插件中添加了 d3。

我运行gatsby develop,并收到以下错误,该错误挂起。

success delete html files from previous builds — 0.005 s
success open and validate gatsby-config.js — 0.003 s
(node:8725) UnhandledPromiseRejectionWarning: Unhandled promise rejection 
(rejection id: 2): Error: Unable to find plugin "d3"
(node:8725) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated.
 In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

任何反馈都会有所帮助,如果这是一个棘手的壮举,那么我实现 d3 集成和简单的个人站点框架目标的阻力最小的路径是什么?

更新 09/08/17

我切换到Hello World!调试 d3 问题的教程。我已经尝试过d3d3-node npm 包。

添加import d3 from "d3"到我的 index.js 文件后,我得到两个在引导完成后发生的类似错误。

两个错误都在编译尝试中循环并分别输出:

d3 错误

ERROR  Failed to compile with 2 errors  
These dependencies were not found:

* child_process in ./~/xmlhttprequest/lib/XMLHttpRequest.js

* fs in ./~/xmlhttprequest/lib/XMLHttpRequest.js

To install them, you can run: npm install --save child_process fs

d3-node 错误 将 index.js 上的导入切换为“d3-node”

ERROR  Failed to compile with 13 errors

These dependencies were not found:

* fs in ./~/jsdom/lib/jsdom.js, ./~/jsdom/lib/jsdom/browser/resource-loader.js and 3 others
* net in ./~/tough-cookie/lib/cookie.js, ./~/forever-agent/index.js and 1 other
* child_process in ./~/jsdom/lib/jsdom/living/xmlhttprequest.js
* tls in ./~/forever-agent/index.js, ./~/tunnel-agent/index.js

To install them, you can run: npm install --save fs net child_process tls

These relative modules were not found:

* ../file-api/FileReader-impl.js in ./~/jsdom/lib/jsdom/living/generated/FileReader.js
* ../events/MutationEvent-impl.js in ./~/jsdom/lib/jsdom/living/generated/MutationEvent.js
4

2 回答 2

8

问题与最新版本的 D3 v4 使用一堆 node.js 依赖项有关,正如 gatsby github 问题中所解释的那样:

https://github.com/gatsbyjs/gatsby/issues/2107

解决方案是修改您的 webpack 配置,以便它加载正确版本的 d3。

我在一个 gatsby 项目中使用 D3 来创建一个力导向图,我发现我可以通过只加载 d3-force 库而不是整个 d3 包来回避这个问题。这无论如何都是可取的,因为让 D3 直接操作 DOM 是反反应的。更好的方法是使用 D3 进行计算并对渲染做出反应,如下所示:

https://medium.com/walmartlabs/d3v4-forcesimulation-with-react-8b1d84364721

于 2017-11-02T01:00:58.660 回答
5

只有 Gatsby 插件应该添加到 gatsby-config.js。对于像 D3 这样的其他常规 NPM 包,您需要做的就是将它们导入到您的模块中。

于 2017-09-07T05:50:45.737 回答