49

When trying to run the command using nextjs npm run dev shows error - failed to load SWC binary see more info here: https://nextjs.org/docs/messages/failed-loading-swc.

I've tried uninstalling node and reinstalling it again with version 16.13 but without success, on the vercel page, but unsuccessful so far. Any tips?

Also, I noticed it's a current issue on NextJS discussion page and it has to do with the new Rust-base compiler which is faster than Babel.

juliomalves
  • 42,130
  • 20
  • 150
  • 146

21 Answers21

106

Delete the package-lock.json file and the node_modules directory in your project and then run npm install on your terminal.

ouflak
  • 2,458
  • 10
  • 44
  • 49
Joon Kyoung
  • 1,084
  • 2
  • 7
  • 2
16

This worked as suggeted by nextJS docs but it takes away Rust compiler and all its benefits... Here is what I did for those who eventually get stuck...

Step 1. add this line or edit next.json.js

{
swcMinify: false // it should be false by default 
}

Step 2. add a ".babelrc" file to project root dir

Step 3. add this snippet to the new file ".babelrc"

{
"presets": ["next/babel"]
}

Step 4, you need to run this command as steps 1-3 will remove SWC failed to load error but you will notice another error when you run the build command. So run this too

npm install next@canary

hope this helps

8

If you use Docker, just add RUN npm install -D @swc/cli @swc/core to Dockerfile.

5

The best way to fixed this problem on Windows is install "Microsoft Visual C++ Redistributable"

The error is occurring because Next.js is using Rust-based compiler SWC to compile JavaScript/TypeScript now and for this SWC requires a binary be downloaded that is compatible specific to your system.

To Solve this problem :

Just go to Microsoft Visual C++ Redistributable to download latest supported Microsoft Visual C++ Redistributable.

Or, you can simply download from here (please check your version first)

Permalink for latest supported x64 version

The X64 Redistributable package contains both ARM64 and X64 binaries. This package makes it easy to install required Visual C++ ARM64 binaries when the X64 Redistributable is installed on an ARM64 device.

Permalink for latest supported x86 version

Permalink for latest supported ARM64 version

SD Shuvo
  • 51
  • 1
  • 3
5

I have the same issue, don't know why, I am using node v18.4.0 next@12.1.6

to fix this issue

just visit this website

https://learn.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-170

install this enter image description here

Ali Nawaz
  • 89
  • 1
  • 6
4

In your NextJS Project you have this file , named .eslintrc.json, In this file

You have following code

{
  "extends": "next/core-web-vitals"
}

Replace it with

{  
   "extends": ["next/babel","next/core-web-vitals"]
}
Alaeddine
  • 49
  • 1
3

This error occurs because next js uses a Rust-based compiler to compile JavaScript which is much faster than babel but this is not compatible with all system architecture, in other to fix this you have to disable this compiler and use the native babel compiler. This is done by creating a .babelrc file in your root directory and adding this code below to the file;

{"presets": ["next/babel"]}

you can check out this link for more details: SWC Failed to Load - NEXTJS DOCS

Rex Omiv
  • 71
  • 1
  • 5
2

I had the same issue on Windows 11. I upgraded NodeJS to 17.0.1. After that, everything works now.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Nur Mongol
  • 21
  • 2
2

make .babelrc in root directory. And add the following code. { "presets": ["next/babel"], "plugins": [["styled-components", { "ssr": true }]] }

Milan Korangi
  • 21
  • 1
  • 2
2

If you are running using Docker, I had to use node:14-buster-slim as my base image to make it work. I got the idea for my working solution from https://github.com/vercel/next.js/discussions/30468#discussioncomment-1598941.

My multi-staged Dockerfile looks like this:


############### Base Image ###############
FROM node:14-buster-slim AS base
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .

############### Build Image ###############
FROM base AS build

ARG app_env=production
ARG app_port=3000

WORKDIR /build
COPY --from=base /app ./

ENV NODE_ENV=${app_env}
ENV PORT=${app_port}
EXPOSE ${app_port}

RUN npm run build

############### Deploy Image ###############
FROM node:14.18.1-alpine AS production

ARG app_env=production
ENV NODE_ENV=${app_env}
WORKDIR /app
COPY --from=build /build/package*.json ./
COPY --from=build /build/.next ./.next
COPY --from=build /build/public ./public
RUN npm install next

EXPOSE 3000
CMD npm run start

If you want to use docker-compose to run your services, the docker-compose.yaml file for running the next dev will look something like this:

version: "3"

services:
  web-server:
    env_file:
      - ./.env
    build:
      context: .
      dockerfile: Dockerfile
      target: base
    command: npm run dev
    container_name: web-server
    restart: always
    volumes:
      - ./:/app
      - /app/node_modules
    ports:
      - "${NODEJS_PORT}:3000"
Amirsalar
  • 648
  • 9
  • 18
2

Remove node_modules directory and package-lock.json

Run npm i to install the dependencies

If you are on MAC OS, you can directly run below command in terminal

rm -rf node_modules && rm package-lock.json && npm i
Yves Boutellier
  • 1,286
  • 9
  • 19
1

Just run 'npm i' or 'yarn' and then restart the server.

Webby Wolf
  • 35
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 01 '21 at 09:28
1

In my case issue was with NextJs version 12.2. I have downgraded it to 12.1.6 and my issue is fixed.

Irfan DANISH
  • 8,349
  • 12
  • 42
  • 67
1

Step 1. add a ".babelrc" file to project root dir

Step 2. add this to the ".babelrc"

{"presets": ["next/babel"]}

step 3:npm run dev

mehdi behvar
  • 179
  • 1
  • 3
  • +1 for this - removing my `yarn-lock` and `node_modules` and then reinstalling didn't work for me whereas this did - thanks! – jimmyNames Aug 02 '23 at 20:41
0

I'm a beginner with next.js and I had the same error. After searching I got a solution to add .babelrc. but using that couldn't get the features of SWC.

Today I got a real solution, using this example project command. When we create our new project, then swc will work as well as no error will be there.

command- npx create-next-app 'your_project_name' --use-npm --example "https://github.com/vercel/next-learn/tree/master/basics/learn-starter"

Let me know if you face any further issues.

JW Geertsma
  • 857
  • 3
  • 13
  • 19
0

Best solution for this problem is

  1. Delete your package-lock.json.
  2. Downgrade your next.js version "12.1.6" from current version.
  3. run npm i --force command.
  4. now run npm run dev command and it will work.
0

Solution for Attempted to load @next/swc-darwin-arm64, but it was not installed warning while using pnpm:

I deleted node_modules & pnpm-lock.yaml and added "@next/swc-darwin-arm64": "^13.3.0" as a dependency in package.json:

"dependencies": {
  "@next/swc-darwin-arm64": "^13.3.0",
  ...
}
Sebi
  • 398
  • 3
  • 10
0

I followed this answer + the official NextJs docs and it worked.

The steps I did:

  1. Remove package-lock.json y node_modules
  2. Reinstall deps with npm install --legacy-peer-deps --force
  3. Then run the development environment (a message will be displayed saying that a lockfile without SWC deps has been found and that it's going to be patched ... it's patched automatically)

enter image description here

  1. Reinstall deps with npm install --legacy-peer-deps

Notes:

  • The flag --legacy-peer-deps depends on Node's version your're using and how it uses peer deps.
  • I'm using Node 14.18.1 and NPM 9.6.6 on my project.
  • If you use NVM to manage your Node versions you can update NPM to the latest compatible version with your Node version by running nvm install-latest-npm
call0fcode
  • 123
  • 6
-1

If you read the docs it says you need a distributable file which can be downloaded at https://learn.microsoft.com/en-US/cpp/windows/latest-supported-vc-redist?view=msvc-

General Grievance
  • 4,555
  • 31
  • 31
  • 45
maesstro
  • 49
  • 7
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/31608587) – Ethan Apr 30 '22 at 00:12
  • Op didn't specify that they were running on Windows, so this doesn't answer the question. – Subfuzion Feb 24 '23 at 05:36
-1

Just Download Redistributable C++ 2015

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 29 '22 at 00:51
  • See the answer pattern for reference, there is answer about Redistributable C++ 2015 https://stackoverflow.com/a/70636610/8074416 – Harshad Prajapati Jul 27 '22 at 07:10
  • Op didn't specify that they were running on Windows, so this doesn't answer the question. – Subfuzion Feb 24 '23 at 05:37
-4

This is Happen because of you have uninstalled npm modules or yarn in your project Just Run this command / install node packages you will get your back