snowpack: [BUG] React: Invalid hook call

Bug Report Quick Checklist

  • I am on the latest version of Snowpack & all plugins.
  • I use package manager yarn (Fill in: npm, yarn, pnpm, etc).
  • I run Snowpack on OS Mac (Fill in: Windows, Mac, Linux, etc).
  • I run Snowpack on Node.js v12+

Describe the bug

I created a project with npx create-snowpack-app f7-react-snowpack --template @snowpack/app-template-react-typescript --use-yarn to reproduce the error:

f7-react-snowpack.zip

To start the project, do the following:

  1. yarn install
  2. yarn start

The browser will open with the following error:

snowpack-f7-44fa361ca6dd

The issue is also described in https://github.com/framework7io/framework7/issues/3852. And it seems that this is indeed a snowpack issue.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 10
  • Comments: 18 (1 by maintainers)

Most upvoted comments

I have the same issue on 3.8.3. The root cause is that rollup decided to bundle a copy of React with one of the dependencies (react-cosmos-shared2 in my case). Which is actually an expected behavior: https://rollupjs.org/guide/en/#peer-dependencies

To avoid this, we need to tell Rollup to treat React as an external dependency. There is a built-in packageOptions.external option, but it completely ejects dependency from Snowpack’s build pipeline, which is not what we actually want. Luckily, we can use a custom Rollup plugin:

  packageOptions: {
    polyfillNode: true,
    rollup: {
      plugins: [
        {
          name: "externalize-react",
          options: (options) => {
            const isSnowpackExternal = options.external;
            options.external = (id) => id === "react" || id === "react-dom" || isSnowpackExternal(id);
          }
        }
      ]
    }
  }

(don’t forget to clear the cache to force re-install)

Ideally, I think, this should be configurable in packageOptions (ie., externalPeerDependencies).

can confirm that issue still exists in 3.8.0 😦

I’m experiencing this same bug in my dev server. After inspecting the Sources tab in dev tools, I noticed Snowpack was, indeed, resolving react imports differently in different modules. For example, the hooks error was being thrown from a company common component library. For that library, react was being imported from /_snowpack/pkg/componentLib/common/index-<hash>.js which seemed to be a bundle of whatever dependencies were required for that component.

But react-dom is resolving react imports to _snowpack/pkg/react.v17.0.2.js which is expected. I noticed that the common components lib isn’t specifying react as a peerDependency… so when I manually updated its package.json in node_modules, snowpack started to resolve react imports correctly. So it seems Snowpack is relying on peerDependencies (too much?) when computing the app’s dependency graph.

I don’t know enough about snowpack’s source code to do anything with this info… but hopefully this helps someone fix the issue.

Can confirm, got this behaviour with swiper, wasted ton of time, tried everything and anything and blah blah blah. Do you guys have any workaround for now?