a720dbdc4c
Fixes: #38232 Fixes: https://github.com/vercel/next.js/issues/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: https://github.com/vercel/next.js/pull/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`6b8e499c7b/packages/next/lib/get-project-dir.ts (L3)
and it calls a CLI command via6b8e499c7b/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>
136 lines
3.9 KiB
JavaScript
Executable file
136 lines
3.9 KiB
JavaScript
Executable file
#!/usr/bin/env node
|
|
import * as log from '../build/output/log'
|
|
import arg from 'next/dist/compiled/arg/index.js'
|
|
import { NON_STANDARD_NODE_ENV } from '../lib/constants'
|
|
import { commands } from '../lib/commands'
|
|
;['react', 'react-dom'].forEach((dependency) => {
|
|
try {
|
|
// When 'npm link' is used it checks the clone location. Not the project.
|
|
require.resolve(dependency)
|
|
} catch (err) {
|
|
console.warn(
|
|
`The module '${dependency}' was not found. Next.js requires that you include it in 'dependencies' of your 'package.json'. To add it, run 'npm install ${dependency}'`
|
|
)
|
|
}
|
|
})
|
|
|
|
const defaultCommand = 'dev'
|
|
const args = arg(
|
|
{
|
|
// Types
|
|
'--version': Boolean,
|
|
'--help': Boolean,
|
|
'--inspect': Boolean,
|
|
|
|
// Aliases
|
|
'-v': '--version',
|
|
'-h': '--help',
|
|
},
|
|
{
|
|
permissive: true,
|
|
}
|
|
)
|
|
|
|
// Version is inlined into the file using taskr build pipeline
|
|
if (args['--version']) {
|
|
console.log(`Next.js v${process.env.__NEXT_VERSION}`)
|
|
process.exit(0)
|
|
}
|
|
|
|
// Check if we are running `next <subcommand>` or `next`
|
|
const foundCommand = Boolean(commands[args._[0]])
|
|
|
|
// Makes sure the `next --help` case is covered
|
|
// This help message is only showed for `next --help`
|
|
// `next <subcommand> --help` falls through to be handled later
|
|
if (!foundCommand && args['--help']) {
|
|
console.log(`
|
|
Usage
|
|
$ next <command>
|
|
|
|
Available commands
|
|
${Object.keys(commands).join(', ')}
|
|
|
|
Options
|
|
--version, -v Version number
|
|
--help, -h Displays this message
|
|
|
|
For more information run a command with the --help flag
|
|
$ next build --help
|
|
`)
|
|
process.exit(0)
|
|
}
|
|
|
|
const command = foundCommand ? args._[0] : defaultCommand
|
|
const forwardedArgs = foundCommand ? args._.slice(1) : args._
|
|
|
|
if (args['--inspect'])
|
|
throw new Error(
|
|
`--inspect flag is deprecated. Use env variable NODE_OPTIONS instead: NODE_OPTIONS='--inspect' next ${command}`
|
|
)
|
|
|
|
// Make sure the `next <subcommand> --help` case is covered
|
|
if (args['--help']) {
|
|
forwardedArgs.push('--help')
|
|
}
|
|
|
|
const defaultEnv = command === 'dev' ? 'development' : 'production'
|
|
|
|
const standardEnv = ['production', 'development', 'test']
|
|
|
|
if (process.env.NODE_ENV) {
|
|
const isNotStandard = !standardEnv.includes(process.env.NODE_ENV)
|
|
const shouldWarnCommands =
|
|
process.env.NODE_ENV === 'development'
|
|
? ['start', 'build']
|
|
: process.env.NODE_ENV === 'production'
|
|
? ['dev']
|
|
: []
|
|
|
|
if (isNotStandard || shouldWarnCommands.includes(command)) {
|
|
log.warn(NON_STANDARD_NODE_ENV)
|
|
}
|
|
}
|
|
|
|
;(process.env as any).NODE_ENV = process.env.NODE_ENV || defaultEnv
|
|
;(process.env as any).NEXT_RUNTIME = 'nodejs'
|
|
|
|
// In node.js runtime, react has to be required after NODE_ENV is set,
|
|
// so that the correct dev/prod bundle could be loaded into require.cache.
|
|
const { shouldUseReactRoot } = require('../server/utils')
|
|
if (shouldUseReactRoot) {
|
|
;(process.env as any).__NEXT_REACT_ROOT = 'true'
|
|
}
|
|
|
|
// x-ref: https://github.com/vercel/next.js/pull/34688#issuecomment-1047994505
|
|
if (process.versions.pnp === '3') {
|
|
const nodeVersionParts = process.versions.node
|
|
.split('.')
|
|
.map((v) => Number(v))
|
|
|
|
if (
|
|
nodeVersionParts[0] < 16 ||
|
|
(nodeVersionParts[0] === 16 && nodeVersionParts[1] < 14)
|
|
) {
|
|
log.warn(
|
|
'Node.js 16.14+ is required for Yarn PnP 3.20+. More info: https://github.com/vercel/next.js/pull/34688#issuecomment-1047994505'
|
|
)
|
|
}
|
|
}
|
|
|
|
// Make sure commands gracefully respect termination signals (e.g. from Docker)
|
|
// Allow the graceful termination to be manually configurable
|
|
if (!process.env.NEXT_MANUAL_SIG_HANDLE) {
|
|
process.on('SIGTERM', () => process.exit(0))
|
|
process.on('SIGINT', () => process.exit(0))
|
|
}
|
|
|
|
commands[command]()
|
|
.then((exec) => exec(forwardedArgs))
|
|
.then(() => {
|
|
if (command === 'build') {
|
|
// ensure process exits after build completes so open handles/connections
|
|
// don't cause process to hang
|
|
process.exit(0)
|
|
}
|
|
})
|