amplify-hosting icon indicating copy to clipboard operation
amplify-hosting copied to clipboard

Nextjs SSR Build Fail in yarn monorepo

Open itseasy21 opened this issue 3 years ago • 4 comments

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...

itseasy21 avatar Jun 27 '22 09:06 itseasy21

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?

ghost avatar Jun 30 '22 16:06 ghost

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 avatar Jul 03 '22 04:07 itseasy21

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

itseasy21 avatar Jul 19 '22 09:07 itseasy21

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...

gferreri avatar Aug 06 '22 15:08 gferreri

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 avatar May 05 '23 04:05 9oelM

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

9oelM avatar May 05 '23 12:05 9oelM

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

itseasy21 avatar May 10 '23 08:05 itseasy21

My team also went to Vercel because of this.

gferreri avatar May 10 '23 15:05 gferreri

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 avatar May 10 '23 21:05 ghost

⚠️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[bot] avatar May 10 '23 21:05 github-actions[bot]

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[bot] avatar May 10 '23 22:05 github-actions[bot]