Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nextjs SSR Build Fail in yarn monorepo #2834

Closed
4 tasks done
itseasy21 opened this issue Jun 27, 2022 · 11 comments
Closed
4 tasks done

Nextjs SSR Build Fail in yarn monorepo #2834

itseasy21 opened this issue Jun 27, 2022 · 11 comments
Labels
archived This issue has been locked. bug Something isn't working ssr Server Side Rendering feature

Comments

@itseasy21
Copy link

itseasy21 commented Jun 27, 2022

Before opening, please confirm:

App Id

d1trrhbw32iorq

Region

us-east-1

Amplify Hosting feature

Monorepo

Describe the bug

We have a monorepo setup with 2 nextjs packages and 1 common UI packages.

The issue:

While installing the monorepo, yarn by default installs the packages in node_modules folder at the root directory of the repo. When the amplify build starts it is already in packages/package1 directory and everything works fine until the next build finishes and SSR Build starts. At this point, as there is no node_modules in the current directory the SSR Build fails as it can't find next executable.

Workaround

As a workaround, we are executing the command cp -r ../../node_modules . post yarn install which makes the build go through, but this results in various warning messages of circular dependencies.

Expected behavior

The SSR Build should be able to find the next executable from ../../node_modules or there should be a way to set the node_modules path, like maybe using an env var AMPLIFY_NODE_MODULES_DIR where we can set this.

Reproduction steps

Create a yarn monorepo, make sure it's on latest yarn(3.2.1).
Create a monorepo structrure like:

parent_dir
    packages
        common
        app1
        app2

Where app1 and app2 are nextjs projects
Deploy using the attached amplify.yml

Build Settings

version: 1
applications:
  - appRoot: packages/web
    frontend:
      phases:
        preBuild:
          commands:
            - yarn set version 3.2.1
            - yarn install
        build:
          commands:
            - SKIP_PREFLIGHT_CHECK=true yarn build
      artifacts:
        baseDirectory: .next
        files:
            - '**/*'
      cache:
        paths:
          - '../../node_modules/**/*' # Cache `node_modules` for faster `yarn` or `npm i`
          - '.next/cache/**/*' # Cache Next.js for faster application rebuilds
  - appRoot: packages/admin
    frontend:
      phases:
        preBuild:
          commands:
            - yarn set version 3.2.1
            - yarn install
        build:
          commands:
            - SKIP_PREFLIGHT_CHECK=true yarn build
      artifacts:
        baseDirectory: .next
        files:
            - '**/*'
      cache:
        paths:
          - '../../node_modules/**/*' # Cache `node_modules` for faster `yarn` or `npm i`
          - '.next/cache/**/*' # Cache Next.js for faster application rebuilds

Additional information

Yarn Version: 3.2.1
NextJS Version: 12.1

Error Log

Logs
2022-06-27T09:19:35.029Z [INFO]: λ  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
2022-06-27T09:19:35.232Z [INFO]: Starting SSR Build...
2022-06-27T09:20:47.078Z [ERROR]: Error: Command failed with ENOENT: node_modules/.bin/next build
                                spawn node_modules/.bin/next ENOENT
                                at Process.ChildProcess._handle.onexit (node:internal/child_process:283:19)
                                at onErrorNT (node:internal/child_process:478:16)
                                at processTicksAndRejections (node:internal/process/task_queues:83:21) {
                                errno: -2,
                                code: 'ENOENT',
                                syscall: 'spawn node_modules/.bin/next',
                                path: 'node_modules/.bin/next',
                                spawnargs: [ 'build' ],
                                originalMessage: 'spawn node_modules/.bin/next ENOENT',
                                shortMessage: 'Command failed with ENOENT: node_modules/.bin/next build\n' +
                                'spawn node_modules/.bin/next ENOENT',
                                command: 'node_modules/.bin/next build',
                                escapedCommand: '"node_modules/.bin/next" build',
                                exitCode: undefined,
                                signal: undefined,
                                signalDescription: undefined,
                                stdout: '',
                                stderr: '',
                                failed: true,
                                timedOut: false,
                                isCanceled: false,
                                killed: false
                                }
2022-06-27T09:20:47.080Z [ERROR]: �[G�[?25h�[G�[J
                                31s › d1trrhbw32iorq › Error: Command failed with ENOENT: node_modules/.bin/next build
                                spawn node_modules/.bin/next ENOENT
                                �[G�[?25h
Terminating logging...
@ghost ghost added the bug Something isn't working label Jun 27, 2022
@ghost ghost self-assigned this Jun 27, 2022
@ghost ghost added the ssr Server Side Rendering feature label Jun 27, 2022
@ghost
Copy link

ghost commented Jun 30, 2022

Hi @itseasy21 👋🏽 I tested this out with the same folder structure of your application and did not run into this error. Can you provide a sample monorepo that reproduces this same behavior?

@itseasy21
Copy link
Author

itseasy21 commented Jul 3, 2022

Thanks for looking into this @hloriana
You can use this sample repo which has the same error: https://github.com/itseasy21/nextjs-monorepo-ssr-example
I've attached some more details on that below:

More details

Environment Vars

AMPLIFY_DIFF_DEPLOY | false | All branches
AMPLIFY_MONOREPO_APP_ROOT | packages/app1 | All branches
AMPLIFY_NEXTJS_EXPERIMENTAL_TRACE | true | All branches
_LIVE_UPDATES | [{"name":"Node.js version","pkg":"node","type":"nvm","version":"16.15"},"{\"pkg\":\"next-version\",\"type\":\"internal\",\"version\":\"latest\"}",{"name":"Next.js version","pkg":"next-version","type":"internal","version":"latest"}] | All branches

Amplify App Details

Region: us-east-1
App ID: d11pxckupfg4up

Build Logs

log.txt

@itseasy21
Copy link
Author

Hi, @hloriana! were you able to reproduce the bug with the above repo, do you have any updates?

@gferreri
Copy link

gferreri commented Aug 6, 2022

I've just encountered the exact same issue with a sample nextjs app added to an existing yarn 3 - based monorepo. I think that the problem is that the next executable is actually in ../../node_modules/.bin/next. That said, I've already run yarn build which produces the same artifacts, and I'm not sure why Amplify is attempting to run it again.

2022-08-06T00:21:35.822Z [INFO]: ○  (Static)  automatically rendered as static HTML (uses no initial props)
2022-08-06T00:21:35.990Z [INFO]: Starting SSR Build...
2022-08-06T00:22:58.241Z [ERROR]: Error: Command failed with ENOENT: node_modules/.bin/next build
                                  spawn node_modules/.bin/next ENOENT
                                  at Process.ChildProcess._handle.onexit (node:internal/child_process:282:19)
                                  at onErrorNT (node:internal/child_process:477:16)
                                  at processTicksAndRejections (node:internal/process/task_queues:83:21) {
                                  errno: -2,
                                  code: 'ENOENT',
                                  syscall: 'spawn node_modules/.bin/next',
                                  path: 'node_modules/.bin/next',
                                  spawnargs: [ 'build' ],
                                  originalMessage: 'spawn node_modules/.bin/next ENOENT',
                                  shortMessage: 'Command failed with ENOENT: node_modules/.bin/next build\n' +
                                  'spawn node_modules/.bin/next ENOENT',
                                  command: 'node_modules/.bin/next build',
                                  escapedCommand: '"node_modules/.bin/next" build',
                                  exitCode: undefined,
                                  signal: undefined,
                                  signalDescription: undefined,
                                  stdout: '',
                                  stderr: '',
                                  failed: true,
                                  timedOut: false,
                                  isCanceled: false,
                                  killed: false
                                  }
                                  �[G
2022-08-06T00:22:58.243Z [ERROR]: �[?25h�[G�[J
                                  40s › d29h4tb4zvyozz › Error: Command failed with ENOENT: node_modules/.bin/next build
                                  spawn node_modules/.bin/next ENOENT
                                  �[G�[?25h
Terminating logging...

@9oelM
Copy link

9oelM commented May 5, 2023

ah still running into this problem. how did you manage to solve this problem?

2023-05-05T04:14:46.079Z [INFO]: Starting SSR Build...
2023-05-05T04:16:27.565Z [ERROR]: Error: Command failed with ENOENT: node_modules/.bin/next build
                                  spawn node_modules/.bin/next ENOENT
                                  at ChildProcess._handle.onexit (node:internal/child_process:283:19)
                                  at onErrorNT (node:internal/child_process:476:16)
                                  at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
                                  errno: -2,
                                  code: 'ENOENT',
                                  syscall: 'spawn node_modules/.bin/next',
                                  path: 'node_modules/.bin/next',
                                  spawnargs: [ 'build' ],
                                  originalMessage: 'spawn node_modules/.bin/next ENOENT',
                                  shortMessage: 'Command failed with ENOENT: node_modules/.bin/next build\n' +
                                  'spawn node_modules/.bin/next ENOENT',
                                  command: 'node_modules/.bin/next build',
                                  exitCode: undefined,
                                  signal: undefined,
                                  signalDescription: undefined,
                                  stdout: '',
                                  stderr: '',
                                  failed: true,
                                  timedOut: false,
                                  isCanceled: false,
                                  killed: false
                                  }
2023-05-05T04:16:27.570Z [ERROR]: �[G�[?25h�[G�[J
                                  53s › d1aaf8wi1jpkhs › Error: Command failed with ENOENT: node_modules/.bin/next build
                                  spawn node_modules/.bin/next ENOENT
                                  �[G�[?25h

@9oelM
Copy link

9oelM commented May 5, 2023

@itseasy21 are you still just using the workaround that you mentioned?

@itseasy21
Copy link
Author

@9oelM couldn't get any response from amplify so switched to Vercel

@gferreri
Copy link

My team also went to Vercel because of this.

@ghost
Copy link

ghost commented May 10, 2023

Hi folks, apologies for the oversight here. We recently launched monorepo support for Next.js applications using the following monorepo frameworks:

  • npm Workspace
  • Yarn Workspace
  • pnpm Workspace
  • Turborepo
  • Nx

For anyone who was previously running into this issue, please redeploy your application. I tested this out with the sample app provided above and confirmed that the issue no longer persists.

If anyone runs into another issue with hosting their monorepo application, please feel free to open a new issue with the bug report template and we will gladly assist.

@ghost ghost closed this as completed May 10, 2023
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot added the archived This issue has been locked. label May 10, 2023
@github-actions github-actions bot locked and limited conversation to collaborators May 10, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived This issue has been locked. bug Something isn't working ssr Server Side Rendering feature
Projects
None yet
Development

No branches or pull requests

3 participants