gatsby: Import d3 causes webpack dependency errors
Adding the line import * as d3 from 'd3' to any JS file in my src code causes the following errors:
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
Could this be a bug caused by Gatsby’s webpack config? I don’t understand what’s going on here: Why is XMLHttpRequest throwing an error when it tries to require built in node modules like fs and child_process, and why would these errors be triggered by including d3?
More info
gatsby-config.js
module.exports = {
siteMetadata: {
title: 'NathanShane.me',
author: 'Nathan Shane',
description: require('./package.json').description
},
plugins: [
'gatsby-plugin-react-helmet',
'gatsby-plugin-catch-links',
'gatsby-transformer-sharp',
'gatsby-plugin-offline',
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${__dirname}/src/pages/projects`,
name: 'projects',
},
},
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${__dirname}/src/pages/blog`,
name: 'blogPosts',
},
},
{
resolve: 'gatsby-transformer-remark',
options: {
plugins: [
{
resolve: `gatsby-remark-images`,
options: {
maxWidth: 590
}
},
'gatsby-remark-copy-linked-files'
]
}
}
]
}
gatsby-node.js:
const {resolve} = require('path')
exports.modifyBabelrc = ({ babelrc }) => {
return {
plugins: babelrc.plugins.concat([
['babel-plugin-root-import']
]),
}
}
exports.createPages = ({boundActionCreators, graphql}) => {
const {createPage} = boundActionCreators
return graphql(
`
{
allMarkdownRemark(limit: 1000) {
edges {
node {
fileAbsolutePath
frontmatter {
slug
}
}
}
}
}
`
).then((result) => {
if (result.errors) {
return Promise.reject(result.errors)
}
const nodes = result.data.allMarkdownRemark.edges
const filterByPathIncludes = (testStr, nodes) => (
nodes.filter(
({node: {fileAbsolutePath}}) => (fileAbsolutePath.includes(testStr))
)
)
const projects = filterByPathIncludes('/pages/projects/', nodes)
projects.forEach(({node}) => {
const {slug} = node.frontmatter
createPage({
path: `/projects/${slug}`,
component: resolve('./src/templates/project.js'),
context: {
slug
}
})
})
const blogPosts = filterByPathIncludes('/pages/blog/', nodes)
blogPosts.forEach(({node}) => {
const {slug} = node.frontmatter
createPage({
path: `/blog/${slug}`,
component: resolve('./src/templates/blogPost.js'),
context: {
slug
}
})
})
}).catch((error) => {
console.log(error)
})
}
package.json:
{
"name": "nathan_shane_site",
"description": "Nathan Shane's portfolio website",
"repository": "https://github.com/nwshane/nwshane.github.io.git",
"license": "MIT",
"scripts": {
"dev": "gatsby develop",
"build": "gatsby build",
"start": "gatsby serve"
},
"dependencies": {
"babel-plugin-root-import": "^5.1.0",
"d3": "^4.10.2",
"gatsby": "^1.8.11",
"gatsby-link": "^1.4.1",
"gatsby-plugin-catch-links": "^1.0.8",
"gatsby-plugin-offline": "^1.0.9",
"gatsby-plugin-react-helmet": "^1.0.6",
"gatsby-remark-copy-linked-files": "^1.5.7",
"gatsby-remark-images": "^1.5.10",
"gatsby-source-filesystem": "^1.4.12",
"gatsby-transformer-remark": "^1.7.6",
"gatsby-transformer-sharp": "^1.6.5",
"normalize.css": "^7.0.0",
"ramda": "^0.24.1",
"styled-components": "^2.1.2",
"webfontloader": "^1.6.28"
}
}
Repo and branch in which error is occuring: https://github.com/nwshane/nwshane.github.io/tree/blog
Gatsby version: 1.9.21 NodeJS version: 8.4.0 OS version: Mac OS X 10.12
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 29 (17 by maintainers)
d3-requestdeleted a field calledbrowserthat used to directwebpackto the correct non-node index file. Nowd3-requestexpects themodulefield to be used.Commit: https://github.com/d3/d3-request/commit/d635b894fee995930f7419189255da8e66062710#diff-b9cfc7f2cdf78a7f4b91a753d10865a2L19
Solution:
Add this to the webpack config:
See
packageMainsconfig documentationI’m getting a similar error with fs being required by dotenv, which I presume showed up after reinstalling my node modules a couple of days ago, but I just noticed it today:
@mattjstar’s fix works for me too.
EDIT: No it didn’t - it stopped the error messages from appearing, but then dotenv stopped working.
Some findings:
d3requiresd3-requestcoded3-requestrequiresxmlhttprequestcodexmlhttprequestrequireschild-processandfscoded3-requestbuilds two version - one for node and onemin.nodeversion.d3-requestfrombuild/d3-request.node.js-->build/d3-request.min.jsfixes the import.modifyWebpackConfigto include:node: { fs: 'empty', child_process: 'empty' }fixes the issue.Strange though because this issue seems to not appear when using a very plain
webpack.config.js. Trying to find out whatwebpackdifferences are triggering the error.yep, getting the same issue as @hermionewy when using @haroldtreen 's solution with
packageMains. iTerm output:I was able to get it to work with this, but I’m afraid that will have unintended consequences down the line.