public async start()

in src/common/packager.ts [166:251]


    public async start(resetCache: boolean = false): Promise<void> {
        this.packagerStatusIndicator.updatePackagerStatus(PackagerStatus.PACKAGER_STARTING);
        let executedStartPackagerCmd = false;
        let rnVersion: string;

        if (!(await this.isRunning())) {
            executedStartPackagerCmd = true;

            const versions = await ProjectVersionHelper.getReactNativeVersions(this.projectPath);
            rnVersion = versions.reactNativeVersion;
            await this.monkeyPatchOpnForRNPackager(rnVersion);

            const args = await this.getPackagerArgs(this.projectPath, rnVersion, resetCache);
            //  There is a bug with launching VSCode editor for file from stack frame in 0.38, 0.39, 0.40 versions:
            //  https://github.com/facebook/react-native/commit/f49093f39710173620fead6230d62cc670570210
            //  This bug will be fixed in 0.41
            const failedRNVersions: string[] = ["0.38.0", "0.39.0", "0.40.0"];

            let env = Object.assign({}, process.env);
            // CI="true" env property breaks RN fast refresh feature, so we need to remove it from default env variables
            // See more info in the issue https://github.com/microsoft/vscode-react-native/issues/1529
            delete env.CI;
            if (this.runOptions && (this.runOptions.env || this.runOptions.envFile)) {
                env = GeneralPlatform.getEnvArgument(
                    env,
                    this.runOptions.env,
                    this.runOptions.envFile,
                );
            } else {
                const rootEnv = path.join(this.getProjectPath(), ".env");
                env = GeneralPlatform.getEnvArgument(env, null, rootEnv);
            }

            const reactEnv = Object.assign({}, env, {
                REACT_DEBUGGER: "echo A debugger is not needed: ",
                REACT_EDITOR: !failedRNVersions.includes(rnVersion)
                    ? "code"
                    : this.openFileAtLocationCommand(),
            });

            this.logger.info(localize("StartingPackager", "Starting Packager"));
            // The packager will continue running while we debug the application, so we can"t
            // wait for this command to finish

            const spawnOptions = { env: reactEnv };

            const nodeModulesRoot: string = AppLauncher.getNodeModulesRootByProjectPath(
                this.projectPath,
            );

            const packagerSpawnResult = new CommandExecutor(
                nodeModulesRoot,
                this.projectPath,
                this.logger,
            ).spawnReactPackager(args, spawnOptions);
            this.packagerProcess = packagerSpawnResult.spawnedProcess;

            packagerSpawnResult.outcome.catch(() => {}); // We ignore all outcome errors
        }

        await this.awaitStart();
        if (executedStartPackagerCmd) {
            this.logger.info(localize("PackagerStarted", "Packager started."));
            this.packagerStatus = PackagerStatus.PACKAGER_STARTED;
            void vscode.commands.executeCommand(
                "setContext",
                CONTEXT_VARIABLES_NAMES.IS_RN_PACKAGER_RUNNING,
                true,
            );
        } else {
            this.logger.info(localize("PackagerIsAlreadyRunning", "Packager is already running."));
            if (!this.packagerProcess) {
                this.logger.warning(
                    ErrorHelper.getWarning(
                        localize(
                            "PackagerRunningOutsideVSCode",
                            "React Native Packager running outside of VS Code. If you want to debug please use the 'Attach to packager' option",
                        ),
                    ),
                );
                this.setPackagerStopStateUI();
                return;
            }
        }
        this.packagerStatusIndicator.updatePackagerStatus(PackagerStatus.PACKAGER_STARTED);
    }