vercel / next.js

The React Framework
https://nextjs.org
MIT License
127.04k stars 27k forks source link

Build time error: Can't resolve @swc/helpers/... #48593

Closed jianliao closed 1 year ago

jianliao commented 1 year ago

Verify canary release

Provide environment information

> ./node_modules/.bin/next info

    Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.4.0: Mon Mar  6 20:59:28 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6000
    Binaries:
      Node: 18.15.0
      npm: 9.6.4
      Yarn: N/A
      pnpm: 8.2.0
    Relevant packages:
      next: 13.3.1-canary.16
      eslint-config-next: 13.3.1-canary.16
      react: 18.2.0
      react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

App directory (appDir: true)

Link to the code that reproduces this issue

https://github.com/jianliao/nextjs-rsp-bug

To Reproduce

  1. Git clone https://github.com/jianliao/nextjs-rsp-bug
  2. npm i
  3. npm run build

The issue is exactly same as #38192.

Describe the Bug

After upgrade to next@13.3.1-canary.16, next build will fail with below error messages:

> nextjs-rsp-table@0.1.0 build
> next build

warn  - You have enabled experimental feature (appDir) in next.config.js.
warn  - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk.
info  - Thank you for testing `appDir` please leave your feedback at https://nextjs.link/app-feedback

Failed to compile.

./node_modules/@internationalized/date/dist/import.mjs
Module not found: Can't resolve '@swc/helpers/src/_class_private_field_init.mjs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./node_modules/@react-spectrum/calendar/dist/import.mjs
./node_modules/@adobe/react-spectrum/dist/import.mjs
./app/page.tsx

./node_modules/@internationalized/date/dist/import.mjs
Module not found: Can't resolve '@swc/helpers/src/_define_property.mjs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./node_modules/@react-spectrum/calendar/dist/import.mjs
./node_modules/@adobe/react-spectrum/dist/import.mjs
./app/page.tsx

./node_modules/@react-aria/focus/dist/import.mjs
Module not found: Can't resolve '@swc/helpers/src/_define_property.mjs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./node_modules/@react-spectrum/actiongroup/dist/import.mjs
./node_modules/@adobe/react-spectrum/dist/import.mjs
./app/page.tsx

./node_modules/@react-stately/collections/dist/import.mjs
Module not found: Can't resolve '@swc/helpers/src/_define_property.mjs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./node_modules/@adobe/react-spectrum/dist/import.mjs
./app/page.tsx

./node_modules/@react-stately/grid/dist/import.mjs
Module not found: Can't resolve '@swc/helpers/src/_define_property.mjs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./node_modules/@react-stately/table/dist/import.mjs
./node_modules/@react-spectrum/table/dist/import.mjs
./node_modules/@adobe/react-spectrum/dist/import.mjs
./app/page.tsx

> Build failed because of webpack errors
info  - Creating an optimized production build .%

Expected Behavior

Build success.

Which browser are you using? (if relevant)

Chrome 113.0.5672.35

How are you deploying your application? (if relevant)

next build

jianliao commented 1 year ago

I am pretty sure this issue was introduced by version v13.3.1-canary.12.

joacub commented 1 year ago

You are using probably a monorepo if is the case I have posted one workaround in the meantime

jianliao commented 1 year ago

Thank you for your reply. Yes, our project uses a monorepo package called @adobe/react-spectrum, which is an umbrella package that is used in all the React Spectrum component packages. This is a fairly common pattern for a UI component library. However, it is important to note that Next.js supported this usage until v13.3.1-canary.12. Therefore, we hope that Next.js will continue to support this usage in future versions as well.

jianliao commented 1 year ago

I found a potential related swc-project/swc#5009 in swc-project/swc repo.

jianliao commented 1 year ago

Tried v13.3.1-canary.17, problem still exists.

dvakatsiienko commented 1 year ago

Having this problem in v13.3.1. Using simple turborepo here.

yarn start 2023-04-22 at 2 18 16 PM

aperepel commented 1 year ago

Maybe this could be given extra attention? This literally bombs when trying to follow the Getting Started guide. Thanks!

prodkt commented 1 year ago

I'm getting the same errors using Turborepo. My additional information mimic for the original reports but I also hopped over to a machine to confirm the results and those details are below. So I've received the same result on both MacOS and Windows. I, too am starting from 'getting started' so this Next app is brand new, but been a non-starter as this presents itself right out the gate.

Thanks and let me know if I can provide any better detail. I can post my macOS details as needed but they mimic the og post as mentioned earlier.

> prodkt-builder@0.1.0 build C:\ProdktDevelopment\prodkt-cloud\apps\prodkt-builder
> next build

info  - Skipping linting
info  - Checking validity of types...
info  - Creating an optimized production build...
Failed to compile.

../../node_modules/@internationalized/date/dist/import.mjs
Module not found: Can't resolve '@swc/helpers/src/_class_private_field_init.mjs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../../node_modules/@react-aria/i18n/dist/real-module.mjs
../../node_modules/@react-aria/i18n/dist/import.mjs
../../node_modules/@react-aria/select/dist/import.mjs
../../node_modules/@plasmicapp/react-web/dist/react-web.esm.js
./pages/_app.tsx

../../node_modules/@internationalized/date/dist/import.mjs
Module not found: Can't resolve '@swc/helpers/src/_define_property.mjs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../../node_modules/@react-aria/i18n/dist/real-module.mjs
../../node_modules/@react-aria/i18n/dist/import.mjs
../../node_modules/@react-aria/select/dist/import.mjs
../../node_modules/@plasmicapp/react-web/dist/react-web.esm.js
./pages/_app.tsx

../../node_modules/@react-aria/focus/dist/import.mjs
Module not found: Can't resolve '@swc/helpers/src/_define_property.mjs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../../node_modules/@plasmicapp/react-web/dist/react-web.esm.js
./pages/_app.tsx

../../node_modules/@react-stately/collections/dist/import.mjs
Module not found: Can't resolve '@swc/helpers/src/_define_property.mjs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../../node_modules/@plasmicapp/react-web/dist/react-web.esm.js
./pages/_app.tsx

../../node_modules/@react-stately/list/dist/import.mjs
Module not found: Can't resolve '@swc/helpers/src/_define_property.mjs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../../node_modules/@react-stately/select/dist/import.mjs
../../node_modules/@plasmicapp/react-web/dist/react-web.esm.js
./pages/_app.tsx

> Build failed because of webpack errors
 ELIFECYCLE  Command failed with exit code 1.
    Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Home
    Binaries:
      Node: 16.20.0
      npm: N/A
      Yarn: N/A
      pnpm: 8.1.1
    Relevant packages:
      next: 13.3.1
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
castrosu commented 1 year ago

When using 13.2.x everything works fine. This is 4 days open and literally makes the framework unusable, will anyone fix this?

b-bot commented 1 year ago

Getting the same with @internationalized/date - seems related to Adobe packages.

joacub commented 1 year ago

For people looking for a temporary fix you can use this workaround

https://github.com/vercel/next.js/issues/48577

this issue is not only attached to @swc/helpers this issue es related to all packages that has different versions installed in monorepo. This is failing only in this because the helpers change from the v0.4 to the 0.5 the structure so when resolving to the last version that src folder does not exist

also if you use turbopack the issue does not exist.

jianliao commented 1 year ago

@joacub, thanks for sharing your findings and the temporary workaround in #48577. I appreciate your insight on the issue and the connection with monorepos.

  1. I also confirmed that all the @adobe/react-* packages depend on @swc/helpers@^0.4.14 and that Next.js upgraded to @swc/helpers@0.5.0 in its canary v13.3.1-canary.12 release.
  2. I find it surprising that this is not considered a breaking change from the point of view of the @swc/helpers library. As you mentioned, the package folder structure has completely changed, and all the package entry points are different. This could potentially cause compatibility issues for dependent packages.
  3. Regarding the bug reproducer repo I posted in this Github issue, I found that even when installing the npm package using --install-strategy=nested, the build still fails with the same reason.

It's worth noting that if the issue doesn't exist when using turbopack, it could be an alternative solution for some users. However, it would be helpful if the @swc/helpers maintainers could clarify whether the changes between v0.4 and v0.5 should be considered breaking and if there is a recommended migration path for dependent packages.

jianliao commented 1 year ago
image
joacub commented 1 year ago

@joacub, thanks for sharing your findings and the temporary workaround in #48577. I appreciate your insight on the issue and the connection with monorepos.

  1. I also confirmed that all the @adobe/react-* packages depend on @swc/helpers@^0.4.14 and that Next.js upgraded to @swc/helpers@0.5.0 in its canary v13.3.1-canary.12 release.
  2. I find it surprising that this is not considered a breaking change from the point of view of the @swc/helpers library. As you mentioned, the package folder structure has completely changed, and all the package entry points are different. This could potentially cause compatibility issues for dependent packages.
  3. Regarding the bug reproducer repo I posted in this Github issue, I found that even when installing the npm package using --install-strategy=nested, the build still fails with the same reason.

It's worth noting that if the issue doesn't exist when using turbopack, it could be an alternative solution for some users. However, it would be helpful if the @swc/helpers maintainers could clarify whether the changes between v0.4 and v0.5 should be considered breaking and if there is a recommended migration path for dependent packages.

In monorepos is valid to have different versions not compatible each other, Webpack or nextjs is just not resolving correctly to the package.json node_modules relative to that package, the package exist and it is installed, webpack is just not doing right or next i don’t know who , that’s why the plug-in in Webpack resolved correctly which is a pretty easy solution and I’m really suprised that Webpack is not resolving correctly as that’s how webpack resolved usually. but I don’t know ….

kdy1 commented 1 year ago

I think @swc/helpers@v0.4.x and @swc/helpers@v0.5.x is merged without checking semver compatibility.

kdy1 commented 1 year ago

@jianliao A change from v0.4.x to v0.5.x is a breaking change.

ap211unitech commented 1 year ago

This error is geting fixed by using nextjs version 13.2.4

https://stackoverflow.com/questions/76083438/module-not-found-cant-resolve-swc-helpers-src-class-private-field-init-mjs/76087179#76087179

castrosu commented 1 year ago

This error is geting fixed by using nextjs version 13.2.4

https://stackoverflow.com/questions/76083438/module-not-found-cant-resolve-swc-helpers-src-class-private-field-init-mjs/76087179#76087179

Just like I said, if you use 13.2.x it works. Just not a great of a "fix" ah ah just use a version from couple months ago

aspirisen commented 1 year ago

I had such error for react-aria on "next": "13.3.1". Downgrading to "next": "13.3.0" helped.

P.S. probably you need to set exact version, if you already have 13.3.1 in lock file changing version from "next": "^13.3.1" to "^13.3.0". will probably make no effect since the versions are compatible because of ^

castrosu commented 1 year ago

I had such error for react-aria on "next": "13.3.1". Downgrading to "next": "13.3.0" helped.

P.S. probably you need to set exact version, if you already have 13.3.1 in lock file changing version from "next": "^13.3.1" to "^13.3.0". will probably make no effect since the versions are compatible because of ^

yeah, I also noticed that some early version work if you don't use the ^

b-bot commented 1 year ago

I had such error for react-aria on "next": "13.3.1". Downgrading to "next": "13.3.0" helped.

P.S. probably you need to set exact version, if you already have 13.3.1 in lock file changing version from "next": "^13.3.1" to "^13.3.0". will probably make no effect since the versions are compatible because of ^

Good tip actually, I was under the assumption I had to go back to 12.2.x because I use ~ which uses latest patch version. So ~13.3.0 = 13.3.1

2hmad commented 1 year ago

I tried almost all solutions but the same issue! did anyone resolve it?

error - ./node_modules/@internationalized/date/dist/import.mjs:1:0
Module not found: Can't resolve '@swc/helpers/src/_class_private_field_init.mjs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./node_modules/@react-aria/i18n/dist/real-module.js
./node_modules/@react-aria/i18n/dist/module.js
./node_modules/@nextui-org/react/esm/index.js
./src/providers/index.tsx
<w> [webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: ENOENT: no such file or directory, rename 'xx/.next/cache/webpack/client-development-fallback/0.pack_' -> 'xx/.next/cache/webpack/client-development-fallback/0.pack'
2hmad commented 1 year ago

I had such error for react-aria on "next": "13.3.1". Downgrading to "next": "13.3.0" helped.

P.S. probably you need to set exact version, if you already have 13.3.1 in lock file changing version from "next": "^13.3.1" to "^13.3.0". will probably make no effect since the versions are compatible because of ^

Thanks! Resolved ✅

dashkevych commented 1 year ago

I am also experiencing similar issue with node_modules/next/node_modules/@swc/helpers in Next.js 13.3.1:

error - node_modules/next/node_modules/@swc/helpers/lib/_interop_require_wildcard.js (5:0) @ eval
error - TypeError: Cannot redefine property: default
    at Function.defineProperty (<anonymous>)

Installing Next.js 13.3.0 makes the issue disappear.

kdy1 commented 1 year ago

https://github.com/vercel/next.js/blob/2f6ff0dab330b854c43a1773be2fe739c3582419/packages/next/src/build/webpack-config.ts#L1070-L1072

This is the cause. There's no logic for respecting the semver of @swc/helpers.

@magic-akari How do you think about exposing @swc/helpers/src/xxx.mjs along with the new helpers (@swc/helpers/_/*) to workaround this issue (for a while)?

Technically, this is not a problem of SWC, but I'm not sure if specifying semver in resolve.alias of webpack is possible.

joacub commented 1 year ago

https://github.com/vercel/next.js/blob/2f6ff0dab330b854c43a1773be2fe739c3582419/packages/next/src/build/webpack-config.ts#L1070-L1072

This is the cause. There's no logic for respecting the semver of @swc/helpers.

@magic-akari How do you think about exposing @swc/helpers/src/xxx.mjs along with the new helpers (@swc/helpers/_/*) to workaround this issue (for a while)?

Technically, this is not a problem of SWC, but I'm not sure if specifying semver in resolve.alias of webpack is possible.

I have posted a solution in the meantime this get resolved, look here for the webpack simple plugin

here just in case:

https://github.com/vercel/next.js/issues/48593#issuecomment-1519111776

kdy1 commented 1 year ago

@joacub I'm not asking for a workaround. I'm the author of the swc project and I'm working to fix this issue.

joacub commented 1 year ago

@joacub I'm not asking for a workaround. I'm the author of the swc project.

o sorry didn't saw that, good, in my understanding there is nothing wrong in you library, webpack is enough smart to resolve the correct package with many different versions with no collision.

that line that you posted force every one to use the same @swc/helpers version. they should remove if is possible.

magic-akari commented 1 year ago

https://github.com/vercel/next.js/blob/2f6ff0dab330b854c43a1773be2fe739c3582419/packages/next/src/build/webpack-config.ts#L1070-L1072

This is the cause. There's no logic for respecting the semver of @swc/helpers.

@magic-akari How do you think about exposing @swc/helpers/src/xxx.mjs along with the new helpers (@swc/helpers/_/*) to workaround this issue (for a while)?

Technically, this is not a problem of SWC, but I'm not sure if specifying semver in resolve.alias of webpack is possible.

It's a simple task to add src/*.mjs entry back. I find that you have submitted #48806 . Should we still need to fix this issue in @swc/helpers? @kdy1

kdy1 commented 1 year ago

I think https://github.com/vercel/next.js/pull/48806 will not work, so I'm still considering fixing it from @swc/helpers side

kdy1 commented 1 year ago

Can you try updating @swc/helpers to v0.5.1?

https://github.com/vercel/next.js/pull/48808 will change the default to v0.5.1, but in the meantime you can update @swc/helpers

yousufislamme commented 1 year ago

When I try to use any of UI libraries in Next.js, this error shows me error
https://prnt.sc/_YY1SozdFlSw

tuxracer commented 1 year ago

@yousufislamme try adding:

  "overrides": {
    "next@13.3.1": {
      "@swc/helpers": "0.5.1"
    }
  }

to your package.json

jianliao commented 1 year ago

Confirmed. The issue had been fixed on next@13.3.2-canary.6 for my reproducer repo.

joacub commented 1 year ago

just to note that also this version seems like there is a huge improvement in building times, I was experiencing inside docker cold build times of 6 seconds to 20 seconds, and now seems like rock solid 2 seconds, still high but much better.

huykon commented 1 year ago

do we have any release versions to fix this issue?

huozhi commented 1 year ago

@huykon It's released in v13.3.2-canary.6

clearly-outsane commented 1 year ago

for some reason im getting this error in next v13.2.4 . It was working until I installed @react-aria/i18n, I can't figure out why

philwolstenholme commented 1 year ago

@yousufislamme try adding:

  "overrides": {
    "next@13.3.1": {
      "@swc/helpers": "0.5.1"
    }
  }

to your package.json

Does this give anyone else this error?

npm ERR! code EOVERRIDE
npm ERR! Override for next@^13.3.1 conflicts with direct dependency
charltoons commented 1 year ago

@clearly-outsane I'm seeing the same thing with @react-aria/focus. I'm having trouble creating a minimal repro to offer the Next.js team though, can you? And are you using a monorepo by chance?

In 13.3.2-canary.11 the build works, but I get the following runtime error for anything in the appDir:

error - ../../node_modules/@react-aria/focus/dist/main.js (687:0) @ new $a7a032acae3ddda9$var$Tree
error - TypeError: (0 , $parcel$interopDefault(...)) is not a function

And here's the line in @react-aria/focus where the failure is happening, note it's still @swc/helpers.

(0, ($parcel$interopDefault($aB6Cp$swchelperslib_define_propertyjs)))(this, "children", []);
kdy1 commented 1 year ago

I'll try another fix

salamaashoush commented 1 year ago

I still have this issue with Jest and @swc/jest also errors coming from the react-aria packages

dvakatsiienko commented 1 year ago

@salamaashoush You may have try the recently released Next.js of version 13.3.2. All the errors mentioned above was resolved in this version. I have no errors with this new version of Next.js.

camcam2605 commented 1 year ago

v13.3.2 officially released to solve this issue. See: https://github.com/vercel/next.js/releases/tag/v13.3.2

salamaashoush commented 1 year ago

yeah maybe I should report it on the react-aria or swc repo since I am not using nextjs and I have the same error but with jest, any help @kdy1?

pierratono commented 1 year ago

Facing this issue on next@13.3.4 after import { NextUIProvider } from '@nextui-org/react' into layout.tsx using appdir

lucassimines commented 1 year ago

When trying to deploy Docker on Ubuntu 22.04 I get the following:

Next version: 13.4.1

#20 36.34 Module not found: Can't resolve './projects/Card'
#20 36.34 
#20 36.34 https://nextjs.org/docs/messages/module-not-found
#20 36.34 
#20 36.34 ./src/app/projects/project/[slug]/Intro.tsx
#20 36.34 Module not found: Can't resolve '@/projects/Tags'
#20 36.34 
#20 36.34 https://nextjs.org/docs/messages/module-not-found
#20 36.34 
#20 36.34 ./src/app/about/page.tsx
#20 36.34 Module not found: Can't resolve './Intro'
#20 36.34 
#20 36.34 https://nextjs.org/docs/messages/module-not-found
#20 36.34 
#20 36.34 ./src/app/about/page.tsx
#20 36.34 Module not found: Can't resolve './Technologies'

But on my Macbook M1 using Docker the errors don't happen

patik commented 1 year ago

I still have this issue with Jest when using Next 13.4.4. The latest Next version that works for me is 13.3.0.

TechQuery commented 1 year ago

How to fix this bug in nextjs@12.x ?

fraincs commented 1 year ago

I still have this issue with Jest when using Next 13.4.4. The latest Next version that works for me is 13.3.0.

I have the issue on 13.3.0 oddly.