Next.js app using @formatjs/swc-plugin won't build on Netlify

Hi, I’m having an issue on deployment for my Next.js app.
The build works fine locally, but not on Netlify.

I’ve researched the error thrown in the logs, it seems it could be related to SWC.
I’m using @formatjs/swc-plugin in my app.

Here’s what my next.config.js looks like:

const rewrites = require("./rewrites.config");

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  pageExtensions: ["page.tsx", "page.ts", "page.jsx", "page.js"],
  experimental: {
    swcPlugins: [
      [
        "@formatjs/swc-plugin",
        {
          excluded: [],
        },
      ],
    ],
  },
  i18n: {
    locales: ["en", "fr"],
    defaultLocale: "fr",
  },
  webpack(config, { dev, ...other }) {
    if (!dev) {
      // https://formatjs.io/docs/guides/advanced-usage#react-intl-without-parser-40-smaller
      config.resolve.alias["@formatjs/icu-messageformat-parser"] =
        "@formatjs/icu-messageformat-parser/no-parser";
    }
    return config;
  },
  rewrites,
};

module.exports = nextConfig;

Any idea on how to solve this?


Site name: venerable-cat-5d67e4

Build logs:

7:42:23 PM: build-image version: 099264affa13d33ffbaea4ccf963ddbead423c30 (focal)
7:42:23 PM: buildbot version: 099264affa13d33ffbaea4ccf963ddbead423c30
7:42:23 PM: Building without cache
7:42:23 PM: Starting to prepare the repo for build
7:42:23 PM: No cached dependencies found. Cloning fresh repo
7:42:23 PM: git clone --filter=blob:none https://github.com/Pickbeam/maison-m
7:42:24 PM: Preparing Git Reference refs/heads/main
7:42:24 PM: Parsing package.json dependencies
7:42:26 PM: Different publish path detected, going to use the one specified in the Netlify configuration file: 'frontend/.next' versus '.next' in the Netlify UI
7:42:26 PM: Starting build script
7:42:26 PM: Installing dependencies
7:42:26 PM: Python version set to 2.7
7:42:26 PM: v16.19.1 is already installed.
7:42:26 PM: Now using node v16.19.1 (npm v8.19.3)
7:42:26 PM: Enabling Node.js Corepack
7:42:27 PM: Started restoring cached build plugins
7:42:27 PM: Finished restoring cached build plugins
7:42:27 PM: Attempting Ruby version 2.7.2, read from environment
7:42:27 PM: Using Ruby version 2.7.2
7:42:27 PM: Using PHP version 8.0
7:42:27 PM: Started restoring cached corepack dependencies
7:42:27 PM: Finished restoring cached corepack dependencies
7:42:27 PM: No npm workspaces detected
7:42:27 PM: Started restoring cached node modules
7:42:27 PM: Finished restoring cached node modules
7:42:28 PM: Installing npm packages using npm version 8.19.3
7:42:45 PM: added 707 packages, and audited 708 packages in 17s
7:42:45 PM: 129 packages are looking for funding
7:42:45 PM:   run `npm fund` for details
7:42:45 PM: 15 vulnerabilities (8 moderate, 4 high, 3 critical)
7:42:45 PM: To address issues that do not require attention, run:
7:42:45 PM:   npm audit fix
7:42:45 PM: Some issues need review, and may require choosing
7:42:45 PM: a different dependency.
7:42:45 PM: Run `npm audit` for details.
7:42:45 PM: npm packages installed
7:42:45 PM: Started restoring cached go cache
7:42:45 PM: Finished restoring cached go cache
7:42:45 PM: go version go1.19.7 linux/amd64
7:42:46 PM: Detected 1 framework(s)
7:42:46 PM: "next" at version "13.2.4"
7:42:46 PM: Section completed: initializing
7:42:47 PM: ​
7:42:47 PM:   Netlify Build                                                 
7:42:47 PM: ────────────────────────────────────────────────────────────────
7:42:47 PM: ​
7:42:47 PM: ❯ Version
7:42:47 PM:   @netlify/build 29.7.1
7:42:47 PM: ​
7:42:47 PM: ❯ Flags
7:42:47 PM:   baseRelDir: true
7:42:47 PM:   buildId: 641f408b7a2d317341fe08ea
7:42:47 PM:   deployId: 641f408b7a2d317341fe08ec
7:42:47 PM: ​
7:42:47 PM: ❯ Current directory
7:42:47 PM:   /opt/build/repo/frontend
7:42:47 PM: ​
7:42:47 PM: ❯ Config file
7:42:47 PM:   No config file was defined: using default values.
7:42:47 PM: ​
7:42:47 PM: ❯ Context
7:42:47 PM:   production
7:42:47 PM: ​
7:42:47 PM: ❯ Installing plugins
7:42:47 PM:    - @netlify/plugin-nextjs@4.32.2
7:42:47 PM:    - @netlify/plugin-lighthouse@4.0.7
7:43:07 PM: ​
7:43:07 PM: ❯ Using Next.js Runtime - v4.32.2
7:43:07 PM: ​
7:43:07 PM: ❯ Loading plugins
7:43:07 PM:    - @netlify/plugin-lighthouse@4.0.7 from Netlify app
7:43:09 PM: ​
7:43:09 PM:   1. @netlify/plugin-nextjs (onPreBuild event)                  
7:43:09 PM: ────────────────────────────────────────────────────────────────
7:43:09 PM: ​
7:43:09 PM: No Next.js cache to restore.
7:43:09 PM: Netlify configuration property "build.environment.NEXT_PRIVATE_TARGET" value changed.
7:43:09 PM: ​
7:43:09 PM: (@netlify/plugin-nextjs onPreBuild completed in 19ms)
7:43:09 PM: ​
7:43:09 PM:   2. Build command from Netlify app                             
7:43:09 PM: ────────────────────────────────────────────────────────────────
7:43:09 PM: ​
7:43:09 PM: $ npm run build
7:43:09 PM: > frontend@0.1.0 build
7:43:09 PM: > next build
7:43:09 PM: warn  - You have enabled experimental feature (swcPlugins) in next.config.js.
7:43:09 PM: warn  - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk.
7:43:09 PM: warn  - No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
7:43:09 PM: info  - Linting and checking validity of types...
7:43:12 PM: info  - Creating an optimized production build...
7:43:12 PM: thread '<unnamed>' panicked at 'failed to invoke plugin: failed to invoke plugin on 'Some("/opt/build/repo/frontend/src/pages/index.page.tsx")'
7:43:12 PM: Caused by:
7:43:12 PM:     0: Cannot compile plugin binary
7:43:12 PM:     1: WebAssembly translation error: Error when converting wat: expected `(`
7:43:12 PM:             --> <anon>:1:1
7:43:12 PM:              |
7:43:12 PM:            1 | "use strict";
7:43:12 PM:              | ^', /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/swc-0.245.27/src/plugin.rs:228:14note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
7:43:12 PM: thread '<unnamed>' panicked at 'failed to invoke plugin: failed to invoke plugin on 'Some("/opt/build/repo/frontend/src/pages/_app.page.tsx")'
7:43:12 PM: Caused by:
7:43:12 PM:     0: Cannot compile plugin binary
7:43:12 PM:     1: WebAssembly translation error: Error when converting wat: expected `(`
7:43:12 PM:             --> <anon>:1:1
7:43:12 PM:              |
7:43:12 PM:            1 | "use strict";
7:43:12 PM:              | ^', /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/swc-0.245.27/src/plugin.rs:228:14thread '<unnamed>' panicked at 'failed to invoke plugin: failed to invoke plugin on 'Some("/opt/build/repo/frontend/src/pages/_document.page.tsx")'
7:43:12 PM: Caused by:
7:43:12 PM:     0: Cannot compile plugin binary
7:43:12 PM:     1: WebAssembly translation error: Error when converting wat: expected `(`
7:43:12 PM:             --> <anon>:1:1
7:43:12 PM:              |
7:43:12 PM:            1 | "use strict";
7:43:12 PM:              | ^', /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/swc-0.245.27/src/plugin.rs:228:14thread '<unnamed>' panicked at 'failed to invoke plugin: failed to invoke plugin on 'Some("/opt/build/repo/frontend/src/pages/about/index.page.tsx")'
7:43:12 PM: Caused by:
7:43:12 PM:     0: Cannot compile plugin binary
7:43:12 PM:     1: WebAssembly translation error: Error when converting wat: expected `(`
7:43:12 PM:             --> <anon>:1:1
7:43:12 PM:              |
7:43:12 PM:            1 | "use strict";
7:43:12 PM:              | ^', /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/swc-0.245.27/src/plugin.rs:228:14thread '<unnamed>' panicked at 'failed to invoke plugin: failed to invoke plugin on 'Some("/opt/build/repo/frontend/src/pages/blog/[slug].page.tsx")'
7:43:12 PM: Caused by:
7:43:12 PM:     0: Cannot compile plugin binary
7:43:12 PM:     1: WebAssembly translation error: Error when converting wat: expected `(`
7:43:12 PM:             --> <anon>:1:1
7:43:12 PM:              |
7:43:14 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2
7:43:12 PM:            1 | "use strict";
7:43:12 PM:              | ^', /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/swc-0.245.27/src/plugin.rs:228:14thread '<unnamed>' panicked at 'failed to invoke plugin: failed to invoke plugin on 'Some("/opt/build/repo/frontend/src/pages/product/[slug].page.tsx")'
7:43:12 PM: Caused by:
7:43:12 PM:     0: Cannot compile plugin binary
7:43:12 PM:     1: WebAssembly translation error: Error when converting wat: expected `(`
7:43:12 PM:             --> <anon>:1:1
7:43:12 PM:              |
7:43:12 PM:            1 | "use strict";
7:43:12 PM:              | ^', /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/swc-0.245.27/src/plugin.rs:228:14thread '<unnamed>' panicked at 'failed to invoke plugin: failed to invoke plugin on 'Some("/opt/build/repo/frontend/node_modules/next/dist/pages/_error.js")'
7:43:12 PM: Caused by:
7:43:12 PM:     0: Cannot compile plugin binary
7:43:12 PM:     1: WebAssembly translation error: Error when converting wat: expected `(`
7:43:12 PM:             --> <anon>:1:1
7:43:12 PM:              |
7:43:12 PM:            1 | "use strict";
7:43:13 PM:              | ^', /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/swc-0.245.27/src/plugin.rs:228:14Failed to compile.
7:43:13 PM: 
7:43:13 PM: ./node_modules/next/dist/pages/_error.js
7:43:13 PM: Error: failed to process failed to invoke plugin: failed to invoke plugin on 'Some("/opt/build/repo/frontend/node_modules/next/dist/pages/_error.js")'
7:43:13 PM: Caused by:
7:43:13 PM:     0: Cannot compile plugin binary
7:43:13 PM:     1: WebAssembly translation error: Error when converting wat: expected `(`
7:43:13 PM:             --> <anon>:1:1
7:43:13 PM:              |
7:43:13 PM:            1 | "use strict";
7:43:13 PM:              | ^
7:43:13 PM: ./src/pages/_app.page.tsx
7:43:13 PM: Error: failed to process failed to invoke plugin: failed to invoke plugin on 'Some("/opt/build/repo/frontend/src/pages/_app.page.tsx")'
7:43:13 PM: Caused by:
7:43:13 PM:     0: Cannot compile plugin binary
7:43:13 PM:     1: WebAssembly translation error: Error when converting wat: expected `(`
7:43:13 PM:             --> <anon>:1:1
7:43:13 PM:              |
7:43:13 PM:            1 | "use strict";
7:43:13 PM:              | ^
7:43:13 PM: ./src/pages/_document.page.tsx
7:43:13 PM: Error: failed to process failed to invoke plugin: failed to invoke plugin on 'Some("/opt/build/repo/frontend/src/pages/_document.page.tsx")'
7:43:13 PM: Caused by:
7:43:13 PM:     0: Cannot compile plugin binary
7:43:13 PM:     1: WebAssembly translation error: Error when converting wat: expected `(`
7:43:13 PM:             --> <anon>:1:1
7:43:13 PM:              |
7:43:13 PM:            1 | "use strict";
7:43:13 PM:              | ^
7:43:13 PM: ./src/pages/about/index.page.tsx
7:43:13 PM: Error: failed to process failed to invoke plugin: failed to invoke plugin on 'Some("/opt/build/repo/frontend/src/pages/about/index.page.tsx")'
7:43:13 PM: Caused by:
7:43:13 PM:     0: Cannot compile plugin binary
7:43:13 PM:     1: WebAssembly translation error: Error when converting wat: expected `(`
7:43:13 PM:             --> <anon>:1:1
7:43:13 PM:              |
7:43:13 PM:            1 | "use strict";
7:43:13 PM:              | ^
7:43:13 PM: ./src/pages/blog/[slug].page.tsx
7:43:13 PM: Error: failed to process failed to invoke plugin: failed to invoke plugin on 'Some("/opt/build/repo/frontend/src/pages/blog/[slug].page.tsx")'
7:43:13 PM: Caused by:
7:43:13 PM:     0: Cannot compile plugin binary
7:43:13 PM:     1: WebAssembly translation error: Error when converting wat: expected `(`
7:43:13 PM:             --> <anon>:1:1
7:43:13 PM:              |
7:43:13 PM:            1 | "use strict";
7:43:13 PM:              | ^
7:43:13 PM: > Build failed because of webpack errors
7:43:13 PM: ​
7:43:13 PM:   "build.command" failed                                        
7:43:13 PM: ────────────────────────────────────────────────────────────────
7:43:13 PM: ​
7:43:13 PM:   Error message
7:43:13 PM:   Command failed with exit code 1: npm run build
7:43:13 PM: ​
7:43:13 PM:   Error location
7:43:13 PM:   In Build command from Netlify app:
7:43:13 PM:   npm run build
7:43:13 PM: ​
7:43:13 PM:   Resolved config
7:43:13 PM:   build:
7:43:13 PM:     base: /opt/build/repo/frontend
7:43:13 PM:     command: npm run build
7:43:13 PM:     commandOrigin: ui
7:43:13 PM:     environment:
7:43:13 PM:       - SANITY_PROJECT_ID
7:43:13 PM:       - NEXT_PRIVATE_TARGET
7:43:13 PM:     publish: /opt/build/repo/frontend/.next
7:43:13 PM:     publishOrigin: ui
7:43:13 PM:   plugins:
7:43:13 PM:     - inputs: {}
7:43:13 PM:       origin: ui
7:43:13 PM:       package: '@netlify/plugin-nextjs'
7:43:13 PM:     - inputs: {}
7:43:13 PM:       origin: ui
7:43:13 PM:       package: '@netlify/plugin-lighthouse'
7:43:13 PM: Caching artifacts
7:43:13 PM: Started saving node modules
7:43:13 PM: Finished saving node modules
7:43:13 PM: Started saving build plugins
7:43:13 PM: Finished saving build plugins
7:43:13 PM: Started saving corepack cache
7:43:13 PM: Finished saving corepack cache
7:43:13 PM: Started saving pip cache
7:43:13 PM: Finished saving pip cache
7:43:13 PM: Started saving emacs cask dependencies
7:43:13 PM: Finished saving emacs cask dependencies
7:43:13 PM: Started saving maven dependencies
7:43:13 PM: Finished saving maven dependencies
7:43:13 PM: Started saving boot dependencies
7:43:13 PM: Finished saving boot dependencies
7:43:13 PM: Started saving rust rustup cache
7:43:13 PM: Finished saving rust rustup cache
7:43:13 PM: Started saving go dependencies
7:43:13 PM: Finished saving go dependencies
7:43:14 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
7:43:14 PM: Failing build: Failed to build site
7:43:14 PM: Finished processing build request in 51.172s

Are you sure? To confirm, you’re talking about npm run build and not npm run dev, right? If yes, have you confirmed all the package versions or Node version is same as local? If yes, please share a minimal reproduction.