Skip to content

Next.js 13: Build failure when enabling appDir and using Yarn PnP #42427

Closed

Description

@shun-shobon

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 21.6.0: Mon Aug 22 20:17:10 PDT 2022; root:xnu-8020.140.49~2/RELEASE_X86_64
Binaries:
  Node: 18.12.0
  npm: 8.19.2
  Yarn: 3.2.4
  pnpm: N/A
Relevant packages:
  next: 13.0.2-canary.2
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

When using Yarn PnP to manage packages, running next bulid with appDir enabled will fail to build.
The build log is:

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

warn  - `enableUndici` option is unnecessary in Node.js v18.0.0 or greater.

> Build error occurred
Error: EROFS: read-only filesystem, open '/node_modules/next/dist/server/initialize-require-hook.js'
    at makeError$1 (/Users/shun_shobon/tmp/my-app/.pnp.cjs:958:24)
    at EROFS (/Users/shun_shobon/tmp/my-app/.pnp.cjs:985:10)
    at ZipFS.prepareWriteFile (/Users/shun_shobon/tmp/my-app/.pnp.cjs:2820:13)
    at ZipFS.writeFilePromise (/Users/shun_shobon/tmp/my-app/.pnp.cjs:2791:55)
    at /Users/shun_shobon/tmp/my-app/.pnp.cjs:3912:26
    at /Users/shun_shobon/tmp/my-app/.pnp.cjs:4136:81
    at ZipOpenFS.getZipPromise (/Users/shun_shobon/tmp/my-app/.pnp.cjs:4243:22)
    at ZipOpenFS.makeCallPromise (/Users/shun_shobon/tmp/my-app/.pnp.cjs:4136:23)
    at ZipOpenFS.writeFilePromise (/Users/shun_shobon/tmp/my-app/.pnp.cjs:3909:23)
    at VirtualFS.writeFilePromise (/Users/shun_shobon/tmp/my-app/.pnp.cjs:3219:24) {
  code: 'EROFS'
}

Looking at the error log, it appears that the attempt to write in next/dist/server/initialize-require-hook.js is failing:

await promises
.writeFile(
initialRequireHookFilePath,
content.replace(
/isPrebundled = (true|false)/,
`isPrebundled = ${isAppDirEnabled}`
)
)

Expected Behavior

No error logs are displayed and the build succeeds.

Link to reproduction

https://github.com/shun-shobon/nextjs-13-yarn-berry-build-reproduction

To Reproduce

  • clone this repo
  • run yarn to install dependencies
  • run yarn build
  • an error log is output and the build fails

NEXT-1378

Activity

  1. added
    bugIssue was opened via the bug report template.
    on Nov 3, 2022
  2. twavv commented on Nov 3, 2022

    @twavv

    I just ran into this. A temporary workaround is to yarn unplug next.

  3. nastynaz commented on Dec 11, 2022

    @nastynaz

    I spent hours trying to figure this out. Bump.

  4. Revolt9k commented on Dec 13, 2022

    @Revolt9k

    Same problem

  5. madjam002 commented on Dec 16, 2022

    @madjam002

    This is also a problem when running ‘next build’ where node_modules is on a read only file system as is the case with some build systems.

  6. cyrilwanner commented on Jan 16, 2023

    @cyrilwanner

    We are also facing this issue with Bazel (rules_js & pnpm) when trying to enable the appDir feature.
    Modifying files within node_modules might work for simple setups, but can cause a lot of issues with more complex and restrictive build systems where node_modules is on a read only file system (for example to avoid any side effects when sharing dependencies between build agents and for guaranteeing hermetic builds).

    It looks like this was introduced in #41687

    @huozhi @ijjk would it be possible to maybe move this file to the .next output folder or avoid writing it in node_modules in another way, e.g. by checking if the appDir feature is enabled differently?
    I am also happy to contribute if there is a common agreement on how this should be fixed.

  7. samwightt commented on Jan 18, 2023

    @samwightt

    Bump, this really needs attention. Huge blocker for anyone using yarn 2.

  8. ranisalt commented on Feb 18, 2023

    @ranisalt

    Not a good idea for a dependency to modify its own source code when ran... Just generate this file elsewhere

  9. power-f-GOD commented on Mar 8, 2023

    @power-f-GOD

    I just faced this issue with pnpm as well.😕 Any concrete solutions yet?

  10. codebycarlos commented on Mar 9, 2023

    @codebycarlos

    Just came across this as well, huge problem for anyone using Yarn's PnP.

  11. faeb187 commented on Mar 22, 2023

    @faeb187

    yarn 3.4.1 @samwightt not only v2 affected.
    node LTS 18.14.1
    next@latest same as with next@v13.2.5-canary.12 (obviously, PNP both default)

    Build error occurred
    Error: EROFS: read-only filesystem, open '/node_modules/next/dist/server/initialize-require-hook.js'

    Workaround (opt-out Yarn PNP and use node_modules again):

    # prerelease use @v13.2.5-canary.12
    npx create-next-app@latest app \
    --ts \
    --eslint \
    --tailwind \
    --experimental-app \
    --src-dir \
    --import-alias "@/*"
    
    # switch to created app/ directory
    cd app
    
    # adds `nodeLinker:node_modules` to `.yarnrc.yml` and runs `yarn install` in one command
    yarn config set nodeLinker node_modules

    Output:
    ➤ YN0000: Successfully set nodeLinker to 'node-modules'

    Now yarn build works again (or you messed up on your own ^^)

  12. bryanjtc commented on Apr 16, 2023

    @bryanjtc

    Trying to use the workaround by unplugging next in a project using module and module resolution node 16 throws this error. My project has type module in the package.json

    Type error: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("../../../app/layout.js")' call instead.
      To convert this file to an ECMAScript module, change its file extension to '.mts' or create a local package.json file with `{ "type": "module" }`.
    
  13. oxc commented on Apr 30, 2023

    @oxc

    Trying to use the workaround by unplugging next in a project using module and module resolution node 16 throws this error.

    @bryanjtc, this is unrelated to Yarn PnP. I've reported this issue with some background and a workaround (enable typedRoutes experimental feature) in #49004

  14. yannbolliger commented on May 11, 2023

    @yannbolliger
    Contributor

    Looks like this fixed for us (with @cyrilwanner) Bazel with rules_js & pnpm, from next@13.4.1. 👍

  15. huozhi commented on Jul 24, 2023

    @huozhi
    Contributor

    Tested the reproduction attached in the description with latest nextjs, it works well for me, will close this issue now.

  16. kgsvg commented on Jul 28, 2023

    @kgsvg

    The issue for me was getInitialProps. I had done it like Home.getInitialProps = async () => {} but it should be exported on the module level like so:

    export default Home() {
    }
    export async function getInitialProps () {
    }
    
  17. github-actions commented on Aug 15, 2023

    @github-actions
    Contributor

    This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

  18. locked as resolved and limited conversation to collaborators on Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue was opened via the bug report template.linear: nextConfirmed issue that is tracked by the Next.js team.locked

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions