How to use detect-port - 9 common examples

To help you get started, we’ve selected a few detect-port examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gatsbyjs / gatsby / lib / utils / startWithDynamicPort.js View on Github external
module.exports = startServer => program => {
  const port = typeof program.port === 'string'
    ? parseInt(program.port, 10)
    : program.port

  const existingProcess = getProcessForPort(port)

  detect(port, (err, _port) => {
    if (err) {
      console.error(err)
      process.exit()
    }

    if (port !== _port) {
      // eslint-disable-next-line max-len
      const question = chalk.yellow(
        `Something is already running on port ${port}.\n${existingProcess ? ` Probably:\n  ${existingProcess}\n` : ''}\nWould you like to run the app at another port instead? [Y/n]`
      )

      return rlInterface.question(question, answer => {
        if (answer.length === 0 || answer.match(/^yes|y$/i)) {
          program.port = _port // eslint-disable-line no-param-reassign
        }
github insin / nwb / src / webpackServer.js View on Github external
function getServerPort(args, intendedPort, cb) {
  detect(intendedPort, (err, suggestedPort) => {
    if (err) return cb(err)
    // No need to prompt if the intended port is available
    if (suggestedPort === intendedPort) return cb(null, suggestedPort)
    // Support use of --force to avoid interactive prompt
    if (args.force) return cb(null, suggestedPort)

    if (args.clear !== false && args.clearConsole !== false) {
      clearConsole()
    }
    console.log(yellow(`Something is already running on port ${intendedPort}.`))
    console.log()
    inquirer.prompt([
      {
        type: 'confirm',
        name: 'run',
        message: 'Would you like to run the app on another port instead?',
github vendure-ecommerce / vendure / packages / create / src / create-vendure-app.ts View on Github external
try {
                    if (usingTs) {
                        // register ts-node so that the config file can be loaded
                        require(path.join(root, 'node_modules/ts-node')).register();
                    }
                    const { populate } = await import(
                        path.join(root, 'node_modules/@vendure/core/dist/cli/populate')
                    );
                    const { bootstrap, DefaultLogger, LogLevel } = await import(
                        path.join(root, 'node_modules/@vendure/core/dist/index')
                    );
                    const { config } = await import(ctx.configFile);
                    const assetsDir = path.join(__dirname, '../assets');

                    const initialDataPath = path.join(assetsDir, 'initial-data.json');
                    const port = await detectPort(3000);
                    const vendureLogLevel =
                        logLevel === 'silent'
                            ? LogLevel.Error
                            : logLevel === 'verbose'
                            ? LogLevel.Verbose
                            : LogLevel.Info;
                    const bootstrapFn = async () => {
                        await checkDbConnection(config.dbConnectionOptions, root);
                        return bootstrap({
                            ...config,
                            port,
                            silent: logLevel === 'silent',
                            dbConnectionOptions: {
                                ...config.dbConnectionOptions,
                                synchronize: true,
                            },
github spacemeshos / smapp / configs / checkPortInUse.js View on Github external
(function checkPortInUse() {
  const port: string = process.env.PORT || '1212';

  detectPort(port, (err: ?Error, availablePort: number) => {
    if (port !== String(availablePort)) {
      throw new Error(`Port "${port}" on "localhost" is already in use. Please use another port. ex: PORT=4343 yarn dev`);
    } else {
      process.exit(0);
    }
  });
})();
github storybookjs / storybook / lib / core / src / server / build-dev.js View on Github external
const getFreePort = port =>
  detectFreePort(port).catch(error => {
    logger.error(error);
    process.exit(-1);
  });
github storybookjs / storybook / scripts / run-registry.js View on Github external
const freePort = port => detectFreePort(port);
github codejamninja / reactant / packages / core / src / config / ports.js View on Github external
async resolve(port = 6001) {
    port = await detectPort(port);
    if (_.includes(this.occupiedPorts, port)) return this.resolve(++port);
    this.occupiedPorts.push(port);
    return port;
  }
}
github umijs / umi / packages / af-webpack / src / choosePort.js View on Github external
export default function choosePort(defaultPort) {
  if (process.env.DETECT_PORT === 'none') {
    return Promise.resolve(defaultPort);
  }

  return detect(defaultPort).then(
    port =>
      new Promise(resolve => {
        if (port === defaultPort) {
          return resolve(port);
        }
        const message =
          process.platform !== 'win32' && defaultPort < 1024 && !isRoot()
            ? `Admin permissions are required to run a server on a port below 1024.`
            : `Something is already running on port ${defaultPort}.`;
        if (isInteractive) {
          clearConsole();
          const existingProcess = getProcessForPort(defaultPort);
          const question = {
            type: 'confirm',
            name: 'shouldChangePort',
            message: `${chalk.yellow(

detect-port

Node.js implementation of port detector

MIT
Latest version published 2 days ago

Package Health Score

81 / 100
Full package analysis

Popular detect-port functions