You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to import a react component from node_modules and when running gatsby build or gatsby develop I get errors.
Relevant information
I've imported hmda-ui using yarn add git://github.com/cfpb/hmda-ui-modules#gatsby. (repo branch)
I'm trying to import this into src/layouts/index.js, import { Header } from 'hmda-ui'. When using this import I see:
Error: component---src-layouts-index-js-04debe56e2a010c78b98.js from UglifyJs
SyntaxError: Unexpected token: name (Header) [./~/hmda-ui/index.js:1,0]
I've also tried importing Header directly, import Header from 'hmda-ui/header/index.jsx'. In this case I see:
Error: ./~/hmda-ui/header/index.jsx
Module parse failed: /Users/wolfea/Documents/projects/gatsby-site/node_modules/hmda-ui/header/index.jsx Unexpected token (13:4)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (13:4)
at Parser.pp$4.raise (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:2221:15)
at Parser.pp.unexpected (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:603:10)
at Parser.pp$3.parseExprAtom (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:1822:12)
at Parser.pp$3.parseExprSubscripts (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:1715:21)
at Parser.pp$3.parseMaybeUnary (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:1692:19)
at Parser.pp$3.parseExprOps (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:1637:21)
at Parser.pp$3.parseMaybeConditional (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:1620:21)
at Parser.pp$3.parseMaybeAssign (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:1597:21)
at Parser.pp$3.parseParenAndDistinguishExpression (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:1861:32)
at Parser.pp$3.parseExprAtom (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:1796:19)
at Parser.pp$3.parseExprSubscripts (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:1715:21)
at Parser.pp$3.parseMaybeUnary (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:1692:19)
at Parser.pp$3.parseExprOps (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:1637:21)
at Parser.pp$3.parseMaybeConditional (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:1620:21)
at Parser.pp$3.parseMaybeAssign (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:1597:21)
at Parser.pp$3.parseExpression (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:1573:21)
at Parser.pp$1.parseReturnStatement (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:839:33)
at Parser.pp$1.parseStatement (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:699:34)
at Parser.pp$1.parseBlock (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:981:25)
at Parser.pp$3.parseFunctionBody (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:2105:24)
at Parser.pp$1.parseFunction (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:1065:10)
at Parser.pp$3.parseExprAtom (/Users/wolfea/Documents/projects/gatsby-site/node_modules/webpack/node_modules/acorn/dist/acorn.js:1810:19)
@ ./src/layouts/index.js 18:13-48
I've converted these components to ES5 but am still unable to get a build working due to the error above.
Is there a different loader required for importing react components (with JSX) from node_modules? (sorry if I'm missing something obvious).
Additionally, I've tested these components by adding a postinstall script: "postinstall": "mkdir ./src/components/hmda-ui && cp -r ./node_modules/hmda-ui/* ./src/components/hmda-ui/"
to copy the components into the Gatsby source, src/components/ and doing that the build works great. I'd just prefer not to have that step. Of course my imports have changed in this case to import { Header } from '../components/hmda-ui' (from index.jsx) or import Header from 'hmda-ui/header/index.js' (direct import). Both cases work with the postinstall script.
So it seems like a difference between the component living in src vs node_modules?
Thanks @m-allanson. We'll have to do some refactoring to get Gatsby v1 working anyway and then v2 would add to that, so I think our best bet is to wait for v2.
Summary
I'm trying to import a react component from
node_modules
and when runninggatsby build
orgatsby develop
I get errors.Relevant information
I've imported
hmda-ui
usingyarn add git://github.com/cfpb/hmda-ui-modules#gatsby
. (repo branch)I'm trying to import this into
src/layouts/index.js
,import { Header } from 'hmda-ui'
. When using this import I see:I've also tried importing
Header
directly,import Header from 'hmda-ui/header/index.jsx'
. In this case I see:I've converted these components to ES5 but am still unable to get a build working due to the error above.
Is there a different loader required for importing react components (with JSX) from node_modules? (sorry if I'm missing something obvious).
Additionally, I've tested these components by adding a
postinstall
script:"postinstall": "mkdir ./src/components/hmda-ui && cp -r ./node_modules/hmda-ui/* ./src/components/hmda-ui/"
to copy the components into the Gatsby source,
src/components/
and doing that the build works great. I'd just prefer not to have that step. Of course my imports have changed in this case toimport { Header } from '../components/hmda-ui'
(from index.jsx) orimport Header from 'hmda-ui/header/index.js'
(direct import). Both cases work with thepostinstall
script.So it seems like a difference between the component living in
src
vsnode_modules
?Environment (if relevant)
npm list gatsby
):gatsby --version
): 1.1.58File contents (if changed)
gatsby-config.js
:package.json
:gatsby-node.js
: N/Agatsby-browser.js
: N/Agatsby-ssr.js
: N/AThe text was updated successfully, but these errors were encountered: