diff --git a/docs/api-reference/cli.md b/docs/api-reference/cli.md index 208d7c9383b0a..cb40bb937a766 100644 --- a/docs/api-reference/cli.md +++ b/docs/api-reference/cli.md @@ -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: @@ -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 diff --git a/packages/next/src/server/next.ts b/packages/next/src/server/next.ts index d56feef577a86..c40753ba549a5 100644 --- a/packages/next/src/server/next.ts +++ b/packages/next/src/server/next.ts @@ -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() diff --git a/test/integration/app-dir-export/test/index.test.ts b/test/integration/app-dir-export/test/index.test.ts index 05c7274ce975c..ccf4606983909 100644 --- a/test/integration/app-dir-export/test/index.test.ts +++ b/test/integration/app-dir-export/test/index.test.ts @@ -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 }, diff --git a/test/integration/app-dir-export/test/start.test.ts b/test/integration/app-dir-export/test/start.test.ts new file mode 100644 index 0000000000000..6f4aa4870c0c3 --- /dev/null +++ b/test/integration/app-dir-export/test/start.test.ts @@ -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.` + ) + }) +})