vite-plugin-react-swc: Bindings not found error

Hey there

If you have this problem, you probably ran into is https://github.com/npm/cli/issues/4828. The bug happens when npm generates the lockfile with an existing node_modules that contains platform-specific dependencies.

Two solutions:

  • Regenerate the lockfile from scratch rm -rf node_modules package-lock.json && npm i
  • Force install the optional dependencies of SWC:
npm install --force --save-optional \
        @swc/core \
        @swc/core-darwin-arm64 \
        @swc/core-darwin-x64 \
        @swc/core-linux-arm-gnueabihf \
        @swc/core-linux-arm64-gnu \
        @swc/core-linux-arm64-musl \
        @swc/core-linux-x64-gnu \
        @swc/core-linux-x64-musl \
        @swc/core-win32-arm64-msvc \
        @swc/core-win32-ia32-msvc \
        @swc/core-win32-x64-msvc

Original post

Running vite we occasionally come across with this error. The way we found to overcome this was to remove node_modules and reinstall it. Do you guys have any ideas what is causing this?

Screenshot 2023-03-24 at 10 03 06

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 25
  • Comments: 52 (12 by maintainers)

Commits related to this issue

Most upvoted comments

OS: macOS Ventura 13.2 (Intel) VITE: 4.2.1 TS: 4.9.3 PLUGIN-REACT-SWC: 3.2.0 NODE: 16.17.0

I got the same error. We have fixed the problem with running this command:

npm i -D @swc/cli @swc/core

In the end just installing swc as described here: https://swc.rs/docs/getting-started

@proddy This was a misconfiguration of peer dependencies, this is now fixed on the latest version

@Tareq1586 You still get the error with the latest version of @swc/core?

Yes, this is https://github.com/npm/cli/issues/4828. The bug happens when npm generates the lockfile with an existing node_modules that contains platform-specific dependencies. Solution should in all cases be to generate while node_modules does not exist:

rm -rf node_modules package-lock.json && npm i

I would close and pin this issue in the repo.

npm i -D @swc/cli @swc/core

That worked for me.

I can confirm I’m experiencing this on an M2 mac. Interestingly enough, I notice data corruption warnings coming from my company’s JFrog Artifactory instance, which acts as a reverse proxy out to the NPMJS registry. Was a corrupted build of the SWC Plugin pushed out at some point?

npm WARN tar zlib: invalid block type
npm WARN tarball tarball data for @swc/core-darwin-arm64@https://my-artifactory-url.com/api/npm/npm-my-company/@swc/core-darwin-arm64/-/core-darwin-arm64-1.3.66.tgz (sha512-UijJsvuLy73vxeVYEy7urIHksXS+3BdvJ9s9AY+bRMSQW483NO7RLp8g4FdTyJbRaN0BH15SQnY0dcjQBkVuHw==) seems to be corrupted. Trying again.
npm WARN tar zlib: invalid block type
npm WARN tarball tarball data for @swc/core-darwin-arm64@https://my-artifactory-url.com/api/npm/npm-my-company/@swc/core-darwin-arm64/-/core-darwin-arm64-1.3.66.tgz (sha512-UijJsvuLy73vxeVYEy7urIHksXS+3BdvJ9s9AY+bRMSQW483NO7RLp8g4FdTyJbRaN0BH15SQnY0dcjQBkVuHw==) seems to be corrupted. Trying again.

For those that are still stuck

Try using the standard @vitejs/plugin-react plugin instead. Falling back to that from the SWC plugin has unblocked me for now:

// vite.config.ts

import react from '@vitejs/plugin-react';
import { defineConfig } from 'vite';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  server: {
    port: 3000,
  },
});

1-click reproduction: https://pr.new/github.com/FossPrime/vslite/tree/repro-swc

I’ll submit a PR upstream to work around this

@sjdemartini Yeah this is totally expected because SWC is using native binaries. supportedArchitectures is not a workaround, that the exact reason why this feature exist in yarn 2+ when using git shared node_modules.

I ran into this same error when: (1) using my Mac to install all packages with yarn, (2) sharing the node_modules with a Docker image (which is running Debian), (3) running the vite server via Docker.

I never had this issue when using the babel-based https://github.com/vitejs/vite-plugin-react plugin. I also did not see this error when running the dev server directly from my Mac.

I was able to resolve it temporarily by opening a shell in the Docker image and running a yarn install, which pointed out that an OS-specific package was missing, which also resulted in a change to @swc/core:

➤ YN0000: ┌ Fetch step
➤ YN0013: │ @swc/core-linux-x64-gnu@npm:1.3.53 can't be found in the cache and will be fetched from the remote registry
➤ YN0000: └ Completed in 2s 456ms
➤ YN0000: ┌ Link step
➤ YN0008: │ esbuild@npm:0.17.18 must be rebuilt because its dependency tree changed
➤ YN0008: │ @swc/core@npm:1.3.53 [ba08b] must be rebuilt because its dependency tree changed

My .yarnrc.yml file was specifying the following, which was what esbuild docs point out should be used when sharing node_modules between platforms https://esbuild.github.io/getting-started/#simultaneous-platforms:

supportedArchitectures:
  os:
    - current
    - linux

But it turns out that’s currently not sufficient for SWC https://github.com/swc-project/swc/issues/2898. The workaround suggested there https://github.com/swc-project/swc/issues/2898#issuecomment-1180845472 of adding the following to supportedArchitectures did the trick, fixing the “Bindings not found” error and Docker-based vite server (no separate yarn install needed for Docker)!

supportedArchitectures:
  os:
    - current
    - linux
  libc:
    - current
    - glibc

Hi, just a quick bump - today we’ve encountered the same issue on a colleague’s laptop. The interesting part is why this didn’t fail on my machine for the last week or so …

Updating @vitejs/plugin-react-swc to latest (eg. npm i -D @vitejs/plugin-react-swc@latest) fixed everything. No need to install swc locally (npm i -D @swc/cli @swc/core)

The culprit is actually Vite CLI. Creating a new project with npm create vite@latest or npm create vite@latest my-vue-app -- --template react-swc-ts uses the v3.0.0.

  "@vitejs/plugin-react-swc": "^3.0.0",
  "vite": "^4.2.0"

Had same issue. Come from nowhere. Was actively working on the project and suddenly i got this error.

Params

  • Macbook Apple Silicon M2
  • Turborepo
  • Vite
  • React
  • npm

Fixed by: Deleting the “package-lock.json” file and all “node_modules” folders. and then reinstall.

Just make sure that by deleting the “package-lock.json” and reinstalling doesn’t break your dependencies by unintentionally installing/updating to major package versions.

Getting this same error. Weirdly enough it behaves differently based on the base docker image of bun. On Apple silicon(arm arch) with Docker desktop.

SWC doesn’t work with: oven/bun:1-alpine SWC works with: oven/bun:1

Why this issue is closed? I am getting same error with latest vite.

OS: macOS Ventura 13.2 (Intel) VITE: 4.2.1 TS: 4.9.3 PLUGIN-REACT-SWC: 3.2.0 NODE: 16.17.0

I got the same error. We have fixed the problem with running this command:

npm i -D @swc/cli @swc/core

In the end just installing swc as described here: https://swc.rs/docs/getting-started

my issues: 👽 Taro v3.5.1

Error: Bindings not found at Compiler.transformSync (/node_modules/@swc/core/index.js:249:15)

when start the taro project, it has been this error, when I run ‘npm i -D @swc/cli @swc/core’, the issue has been solved. Thank you for your answer.

finally what we should do to solve this error? when ever i pull a branch i got this error and to fix it i havt to delete node-modules and yarn install to fix it. and some packages dont get install well after this. like redux toolkit

PR got merged. I think the problem should be fix at this toolimg plugins level.

https://github.com/kat-tax/vslite/pull/14 Heres what it took, 2 lines

one to detect web containers, one to import a fallback.

npm i -D @swc/cli @swc/core instalé las dev dependencies npm i -D @swc/cli @swc/core ,pero el error continuaba, luego reinstalé node_modules y se solucionó.

Thanks everyone for helping on this. Please go like the npm issue.

See the updated initial comment for solutions to workaround this.

Same issue here. Tried npm install -D @swc/core and npm install -D vite-plugin-react-swc@latest as someone mentioned up. But it is not working for me.

Thanks for finding that issue. That’s my exact scenario. I’ll try spelunking npm source this weekend and submitting a PR if I can.

In the meantime, I think recommending installing the optional deps somewhere (README?) would be helpful.

We noticed this problem after “refreshing” package-lock.json, which we do by deleting the file, and running npm install (node_modules not deleted beforehand). The result is npm removes all @swc/core-xxxx entries from the lockfile except the one for the current platform. This causes the Bindings not found error in Vite’s dev server when that lockfile is then used in npm ci or npm install on another machine that isn’t the same platform as the one that refreshed the lockfile.

See minimal reproduction in GitHub and StackBlitz.


The most reliable workaround I found is to force-install the optional dependencies of @swc/core as optional dependencies of your own project (only the dep that is supported on your platform is installed):

npm install --force --save-optional \
        @swc/core-darwin-arm64 \
        @swc/core-darwin-x64 \
        @swc/core-linux-arm-gnueabihf \
        @swc/core-linux-arm64-gnu \
        @swc/core-linux-arm64-musl \
        @swc/core-linux-x64-gnu \
        @swc/core-linux-x64-musl \
        @swc/core-win32-arm64-msvc \
        @swc/core-win32-ia32-msvc \
        @swc/core-win32-x64-msvc

Then check the resulting lockfile into source control.

When starting out a new template, any package manager will install the latest version available (because of the ^ prefix).

I think there is just bad version of the SWC package that was published at some point and the solution is just to force a new version of @swc/core (direct or indirect by updating the plugin one)

@swc/cli should not be needed.npm i -D @swc/core will force a new version of @swc/core to be downloaded, but you can them remove it manually from the package.json because it’s still a dependency of this plugin

Running vite we occasionally come across with this error. The way we found to overcome this was to remove node_modules and reinstall it. Do you guys have any ideas what is causing this?

Screenshot 2023-03-24 at 10 03 06

Reinstalled but not working