Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Gatsby 4 breaks Storybook #36210

Closed
2 tasks done
sqpollen opened this issue Jul 22, 2022 · 9 comments
Closed
2 tasks done

Upgrade to Gatsby 4 breaks Storybook #36210

sqpollen opened this issue Jul 22, 2022 · 9 comments
Labels
type: bug An issue or pull request relating to a bug in Gatsby

Comments

@sqpollen
Copy link

sqpollen commented Jul 22, 2022

Preliminary Checks

Description

I've upgraded to Gatsby 4.0.0 using this guide and updated all gatsby-plugins. Builds are working as expected, but Storybook will not run and returns the following errors.

ModuleNotFoundError: Module not found: Error: Can't resolve '@gatsbyjs/reach-router/lib/utils' in '/Users/me/graingrowers/web/node_modules/gatsby-link/dist'
Did you mean 'utils.js'?
BREAKING CHANGE: The request '@gatsbyjs/reach-router/lib/utils' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.

I've upgraded Storybook to latest version as part of troubleshooting and the error still occurs.

Here is package.json

{
...
  },
  "dependencies": {
    "@reach/dialog": "^0.17.0",
    "@reach/skip-nav": "^0.15.0",
    "@sanity/block-content-to-react": "^3.0.0",
    "algoliasearch": "^4.10.3",
    "aws-cli": "^0.0.2",
    "aws-sdk": "^2.1026.0",
    "axios": "^0.21.1",
    "bulma": "^0.9.3",
    "date-fns": "^2.22.1",
    "dotenv": "^10.0.0",
    "fast-csv": "^4.3.6",
    "gatsby": "^4.0.0",
    "gatsby-plugin-algolia": "^0.26.0",
    "gatsby-plugin-bugherd": "^1.0.2",
    "gatsby-plugin-canonical-urls": "^4.19.0",
    "gatsby-plugin-google-gtag": "^4.19.0",
    "gatsby-plugin-google-tagmanager": "^4.19.0",
    "gatsby-plugin-image": "^2.19.0",
    "gatsby-plugin-manifest": "^4.19.0",
    "gatsby-plugin-postcss": "^5.19.0",
    "gatsby-plugin-react-helmet": "^5.19.0",
    "gatsby-plugin-robots-txt": "^1.7.1",
    "gatsby-plugin-rollbar": "^1.1.0",
    "gatsby-plugin-rollbar-deploy": "^1.0.0",
    "gatsby-plugin-s3": "^0.3.8",
    "gatsby-plugin-sass": "^5.19.0",
    "gatsby-plugin-sharp": "^4.19.0",
    "gatsby-plugin-svgr": "^3.0.0-beta.0",
    "gatsby-remark-images-contentful": "^5.1.0",
    "gatsby-source-contentful": "^6.1.4",
    "gatsby-source-filesystem": "^3.14.0",
    "gatsby-source-sanity": "^7.0.7",
    "gatsby-transformer-sharp": "^3.14.0",
    "gatsby-transformer-sqip": "^4.1.4",
    "intersection-observer": "^0.12.0",
    "luxon": "^2.3.1",
    "minipass": "2.7.0",
    "prop-types": "^15.7.2",
    "react": "^17.0.2",
    "react-content-loader": "^6.0.3",
    "react-dom": "^17.0.2",
    "react-google-recaptcha": "^2.1.0",
    "react-helmet": "^6.1.0",
    "react-instantsearch-dom": "^6.12.0",
    "react-obfuscate": "^3.6.8",
    "react-player": "^2.9.0",
    "react-select": "^4.3.1",
    "sass": "^1.35.1",
    "sharp": "^0.30.3",
    "svgo": "^2.3.1",
    "url-parse": "^1.5.3"
  },
  "devDependencies": {
    "@babel/core": "^7.14.3",
    "@babel/preset-env": "^7.14.5",
    "@babel/preset-react": "^7.14.5",
    "@sambego/storybook-state": "^2.0.1",
    "@storybook/addon-a11y": "^6.3.6",
    "@storybook/addon-actions": "^6.3.6",
    "@storybook/addon-essentials": "^6.3.6",
    "@storybook/addon-links": "^6.3.6",
    "@storybook/addon-postcss": "^2.0.0",
    "@storybook/builder-webpack5": "^6.3.6",
    "@storybook/manager-webpack5": "^6.3.6",
    "@storybook/preset-scss": "^1.0.3",
    "@storybook/react": "^6.3.6",
    "@svgr/webpack": "^5.5.0",
    "@wojtekmaj/enzyme-adapter-react-17": "^0.6.2",
    "aws-sdk": "^2.1026.0",
    "babel-eslint": "^10.1.0",
    "babel-jest": "^27.0.2",
    "babel-loader": "^8.2.2",
    "babel-preset-gatsby": "^1.7.1",
    "broken-link-checker": "^0.7.8",
    "chai": "^4.3.4",
    "chromatic": "^5.9.1",
    "dotenv": "^10.0.0",
    "dotenv-webpack": "^7.0.2",
    "enzyme": "^3.11.0",
    "eslint": "^7.27.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-config-react-app": "^6.0.0",
    "eslint-plugin-flowtype": "^5.7.2",
    "eslint-plugin-import": "^2.23.4",
    "eslint-plugin-react-hooks": "^4.2.0",
    "fast-csv": "^4.3.6",
    "html-metadata": "^1.7.1",
    "jest": "^27.0.4",
    "lighthouse": "^8.5.1",
    "lint-staged": "^11.0.0",
    "lmdb-store": "^1.6.11",
    "lost": "^8.3.1",
    "mocha": "^9.1.3",
    "pa11y": "^6.0.1",
    "postcss": "^8.3.5",
    "postcss-calc": "^8.0.0",
    "postcss-cli": "^8.3.1",
    "postcss-easy-import": "^3.0.0",
    "postcss-mixins": "^6.2.3",
    "postcss-preset-env": "^6.7.0",
    "postcss-rem": "^2.0.1",
    "prettier": "^2.3.0",
    "react-test-renderer": "^17.0.2",
    "replace": "^1.2.1",
    "start-server-and-test": "^1.12.1",
    "stylelint": "^13.13.1",
    "stylelint-config-prettier": "^8.0.2",
    "stylelint-config-standard": "^22.0.0",
    "webpack": "^5.39.0"
  },
  "browserslist": [
    "defaults"
  ]
}

Reproduction Link

https://gatsby.dev/reproduction

Steps to Reproduce

Install from above package.json

Expected Result

Storybook compiles

Actual Result

Storybook produces above error

Environment

System:
    OS: macOS 12.4
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.3.0 - ~/.nvm/versions/node/v16.3.0/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v14.18.3/bin/yarn
    npm: 7.15.1 - ~/.nvm/versions/node/v16.3.0/bin/npm
  Browsers:
    Chrome: 103.0.5060.134
    Firefox: 101.0.1
    Safari: 15.5
  npmPackages:
    gatsby: ^4.0.0 => 4.0.0
    gatsby-plugin-algolia: ^0.26.0 => 0.26.0
    gatsby-plugin-bugherd: ^1.0.2 => 1.0.2
    gatsby-plugin-canonical-urls: ^4.19.0 => 4.19.0
    gatsby-plugin-google-gtag: ^4.19.0 => 4.19.0
    gatsby-plugin-google-tagmanager: ^4.19.0 => 4.19.0
    gatsby-plugin-image: ^2.19.0 => 2.19.0
    gatsby-plugin-manifest: ^4.19.0 => 4.19.0
    gatsby-plugin-postcss: ^5.19.0 => 5.19.0
    gatsby-plugin-react-helmet: ^5.19.0 => 5.19.0
    gatsby-plugin-robots-txt: ^1.7.1 => 1.7.1
    gatsby-plugin-rollbar: ^1.1.0 => 1.1.0
    gatsby-plugin-rollbar-deploy: ^1.0.0 => 1.0.0
    gatsby-plugin-s3: ^0.3.8 => 0.3.8
    gatsby-plugin-sass: ^5.19.0 => 5.19.0
    gatsby-plugin-sharp: ^4.19.0 => 4.19.0
    gatsby-plugin-svgr: ^3.0.0-beta.0 => 3.0.0-beta.0
    gatsby-remark-images-contentful: ^5.1.0 => 5.4.0
    gatsby-source-contentful: ^6.1.4 => 6.1.4
    gatsby-source-filesystem: ^3.14.0 => 3.14.0
    gatsby-source-sanity: ^7.0.7 => 7.3.2
    gatsby-transformer-sharp: ^3.14.0 => 3.14.0
    gatsby-transformer-sqip: ^4.1.4 => 4.12.1

Config Flags

FAST_DEV: true,
PRESERVE_WEBPACK_CACHE: true,
PRESERVE_FILE_DOWNLOAD_CACHE: true,
@sqpollen sqpollen added the type: bug An issue or pull request relating to a bug in Gatsby label Jul 22, 2022
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jul 22, 2022
@LekoArts LekoArts removed the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jul 22, 2022
@LekoArts
Copy link
Contributor

Please open an issue with Storybook about this. Thanks!

@LekoArts LekoArts closed this as not planned Won't fix, can't repro, duplicate, stale Jul 22, 2022
@aaronadamsCA
Copy link
Contributor

aaronadamsCA commented Jul 23, 2022

@LekoArts Can you please re-open this? gatsby-link ships an ES module since 4.19, and it contains an illegal import.

This import:

import { resolve } from "@gatsbyjs/reach-router/lib/utils"

Is visible in the shipped source here: https://unpkg.com/[email protected]/dist/index.modern.mjs

import{resolve as o}from"@gatsbyjs/reach-router/lib/utils"

The relevant Webpack error message is:

ERR! Module not found: Error: Can't resolve '@gatsbyjs/reach-router/lib/utils' in '/home/runner/work/nishe-web-apps/nishe-web-apps/node_modules/gatsby/node_modules/gatsby-link/dist'
ERR! Did you mean 'utils.js'?
ERR! BREAKING CHANGE: The request '@gatsbyjs/reach-router/lib/utils' failed to resolve only because it was resolved as fully specified
ERR! (probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
ERR! The extension in the request is mandatory for it to be fully specified.
ERR! Add the extension to the request.

Indeed, you can't import from @gatsbyjs/reach-router/lib/utils without an extension in an .mjs file. You could, however, import from @gatsbyjs/reach-router.

@aaronadamsCA
Copy link
Contributor

For anyone who needs a workaround, add this to your root package.json:

  "resolutions": {
+   "gatsby-link": "4.18.1",
    "lint-staged/supports-color": "8.1.1",

@LekoArts
Copy link
Contributor

It's a matter of correctly defining your webpack configuration to handle this and not immediately break on it, Gatsby's webpack config handles that gracefully. The @gatsbyjs/reach-router package doesn't export resolve at the moment so you can't just change the import to this.

But I'll release a new version of @gatsbyjs/reach-router and change the import.

@aaronadamsCA
Copy link
Contributor

You could, however, import from @gatsbyjs/reach-router.

Sorry @LekoArts, I don't know what I looked at when I wrote this; I checked the types (which still come from @types/reach__router) and saw a root resolve export defined... but clearly that's not true. I must have looked in the wrong place.

It's a matter of correctly defining your webpack configuration to handle this and not immediately break on it, Gatsby's webpack config handles that gracefully.

I agree it's possible, but IMO it's better to ship compliant ES modules that don't require custom end-user configs.

FWIW, would an alternative fix have been to simply update the import statement to import { resolve } from "@gatsbyjs/reach-router/lib/utils.js"? I am still figuring out ES modules myself, too.

Regardless we'll appreciate this fix once it ships, thank you.

@LekoArts
Copy link
Contributor

You can try gatsby@next

@sqpollen
Copy link
Author

Thanks @LekoArts and @aaronadamsCA - got it working using gatsby@next.

Do you know when you'll officially release [email protected] with this fix included @LekoArts ?

@LekoArts
Copy link
Contributor

We release a minor every 2 weeks, so in 7 days

@ovflowd
Copy link

ovflowd commented Aug 1, 2022

Hey, @LekoArts is a release happening today?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

No branches or pull requests

4 participants