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

Double server from CLI #38232

Closed
1 task done
Arctomachine opened this issue Jul 1, 2022 · 5 comments · Fixed by #38274
Closed
1 task done

Double server from CLI #38232

Arctomachine opened this issue Jul 1, 2022 · 5 comments · Fixed by #38274
Labels
bug Issue was opened via the bug report template.

Comments

@Arctomachine
Copy link

Verify canary release

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

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Pro
Binaries:
  Node: 16.15.1
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant packages:
  next: 12.2.1-canary.1
  eslint-config-next: 12.2.0
  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)

CLI

Describe the Bug

I have app.js file in root folder.

const start = require('next/dist/cli/next-start')
start.nextStart()

It starts 2 servers at the same time. It starts dev server instead of production and overwrites contents of .next folder with files of dev server.

> node app.js
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
ready - started server on 0.0.0.0:3000, url: http://localhost:3000

Accessing localhost:3000 returns error 404

Expected Behavior

Only start production server

Link to reproduction

https://github.com/Arctomachine/reproduction-double-server

To Reproduce

  1. Run next build
  2. Run node app.js
@Arctomachine Arctomachine added the bug Issue was opened via the bug report template. label Jul 1, 2022
@bengeois
Copy link

bengeois commented Jul 2, 2022

Also have this issue when deploying on AWS Beanstalk

@QuiiBz
Copy link
Member

QuiiBz commented Jul 3, 2022

I don't think the server is started twice:

The first log is coming from

Log.ready(`started server on ${host}:${app.port}, url: ${appUrl}`)

and the second one is from

Log.ready(`started server on ${state.bindAddr}, url: ${state.appUrl}`)

@Arctomachine
Copy link
Author

require('next/dist/cli/next-start') alone already starts server and gives one output. If you try only this line, you will get fully working dev server.
But in either case, with 2 outputs it starts malfunctioning dev server, while functioning production is expected.

@QuiiBz
Copy link
Member

QuiiBz commented Jul 3, 2022

I searched a bit and the issue first appeared in version 12.1.1-canary.5.

Importing next/dist/cli/next-start without calling nextStart() before this version does not start the server.

edit:
It's a regression from #34836:

When getProjectDir is called, it now also imports next/bin/next.ts, which is itself calling commands[command]().

This command should not be defined, but it fallbacks to defaultCommand, which is dev (that explains why the dev server is also started)

2nd edit:
Opened a PR that fixes the issue: #38274

@kodiakhq kodiakhq bot closed this as completed in #38274 Aug 7, 2022
kodiakhq bot pushed a commit that referenced this issue Aug 7, 2022
#38274)

Fixes: #38232 
Fixes: #36893

Version [12.1.1-canary.5](https://github.com/vercel/next.js/releases/tag/v12.1.1-canary.5) introduced a bug, more specifically this PR: #34836

The issue described in #38232 is that the following code starts both the dev and prod servers:
```js
const start = require('next/dist/cli/next-start')
start.nextStart()
```

I searched a bit and found that `lib/get-project-dir.ts#getProjectDir()` now imports `bin/next.ts`

https://github.com/vercel/next.js/blob/6b8e499c7bf13914cca92f9da1737d358133ee20/packages/next/lib/get-project-dir.ts#L3

and it calls a CLI command via

https://github.com/vercel/next.js/blob/6b8e499c7bf13914cca92f9da1737d358133ee20/packages/next/bin/next.ts#L137

This `command` should not be defined, but it fallbacks to `defaultCommand`, which is `dev` (that explains why the dev server is also started)

This PR moves the `cliCommand` types and `commands` variable to a new separate file instead of `bin/next.ts`, to avoid running a CLI command when we import any file that also imports `lib/get-project-dir.ts`

Not sure how integration tests can be added for this issue, but feel free to tell me.

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
@github-actions
Copy link
Contributor

github-actions bot commented Sep 7, 2022

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants