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

fix: show error when "next start" is used with "output" config #47989

Merged
merged 5 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/api-reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Or using the `PORT` environment variable:
PORT=4000 npx next dev
```

> Note: `PORT` can not be set in `.env` as booting up the HTTP server happens before any other code is initialized.
> Note: `PORT` cannot be set in `.env` as booting up the HTTP server happens before any other code is initialized.

You can also set the hostname to be different from the default of `0.0.0.0`, this can be useful for making the application available for other devices on the network. The default hostname can be changed with `-H`, like so:

Expand All @@ -106,7 +106,9 @@ Or using the `PORT` environment variable:
PORT=4000 npx next start
```

> Note: `PORT` can not be set in `.env` as booting up the HTTP server happens before any other code is initialized.
> Note: `PORT` cannot be set in `.env` as booting up the HTTP server happens before any other code is initialized.

> Note: `next start` cannot be used with `output: 'standalone'` or `output: 'export'`.

### Keep Alive Timeout

Expand Down
11 changes: 11 additions & 0 deletions packages/next/src/server/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ export class NextServer {
private async getServer() {
if (!this.serverPromise) {
this.serverPromise = this.loadConfig().then(async (conf) => {
if (!this.options.dev) {
if (conf.output === 'standalone') {
log.warn(
`"next start" does not work with "output: standalone" configuration. Use "node .next/standalone/server.js" instead.`
)
} else if (conf.output === 'export') {
throw new Error(
`"next start" does not work with "output: export" configuration. Use "npx serve@latest out" instead.`
)
}
}
if (conf.experimental.appDir) {
process.env.NEXT_PREBUNDLED_REACT = '1'
overrideBuiltInReactPackages()
Expand Down
2 changes: 1 addition & 1 deletion test/integration/app-dir-export/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ async function runTests({
}
}

describe('app dir with output export', () => {
describe('app dir with output export (next dev / next build)', () => {
it.each([
{ isDev: true, trailingSlash: false },
{ isDev: true, trailingSlash: true },
Expand Down
60 changes: 60 additions & 0 deletions test/integration/app-dir-export/test/start.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-env jest */

import { join } from 'path'
import fs from 'fs-extra'
import {
check,
File,
findPort,
killApp,
nextBuild,
nextStart,
} from 'next-test-utils'

const appDir = join(__dirname, '..')
const distDir = join(appDir, '.next')
const exportDir = join(appDir, 'out')
const nextConfig = new File(join(appDir, 'next.config.js'))
let app

describe('app dir with output export (next start)', () => {
afterEach(async () => {
await killApp(app)
nextConfig.restore()
await fs.remove(distDir)
await fs.remove(exportDir)
})

it('should error during next start with output export', async () => {
const { code } = await nextBuild(appDir)
expect(code).toBe(0)
const port = await findPort()
let stderr = ''
app = await nextStart(appDir, port, {
onStderr(msg: string) {
stderr += msg || ''
},
})
await check(() => stderr, /error/i)
expect(stderr).toContain(
'"next start" does not work with "output: export" configuration. Use "npx serve@latest out" instead.'
)
})

it('should warn during next start with output standalone', async () => {
nextConfig.replace(`output: 'export'`, `output: 'standalone'`)
const { code } = await nextBuild(appDir)
expect(code).toBe(0)
const port = await findPort()
let stderr = ''
app = await nextStart(appDir, port, {
onStderr(msg: string) {
stderr += msg || ''
},
})
await check(() => stderr, /warn/i)
expect(stderr).toContain(
`warn - "next start" does not work with "output: standalone" configuration. Use "node .next/standalone/server.js" instead.`
)
})
})