CLI Companion

  • Hugging Face CLI
    • login
    • whoami
    • repo create
    • upload
    • download
    • lfs-enable-largefiles
    • scan-cache
    • delete-cache
  • Hapi CLI
    • new
    • start
    • build
    • test
    • plugin create
    • route add
  • Cloudflared
    • tunnel
    • tunnel run
    • tunnel list
    • tunnel delete
    • access
    • access tcp
    • update

    The `hapi new` command is used to generate a new Hapi.js project with a predefined structure and essential files. It's a quick way to bootstrap a new Hapi application, similar to how `express-generator` works for Express or `nest new` for NestJS.

    Syntax

    bash
    hapi new <project-name> [options]

    Arguments

    * `<project-name>`: (Required) The name of the directory where your new Hapi project will be created. This will also typically be used as the name of the project within `package.json`.

    Options

    The Hapi CLI is relatively simple, and the `new` command generally focuses on creating a basic project. Advanced options for different templates or configurations are not typically built into the core `hapi new` command itself. The generated project often includes a basic Hapi server setup, routing, and a `package.json` with Hapi dependencies.

    Usage Examples

    #### 1. Create a basic new Hapi project

    This is the most common use case. It will create a directory with the specified project name and populate it with a default Hapi project structure.

    bash
    hapi new my-hapi-app

    **Explanation:**

    This command will:

    * Create a new directory named `my-hapi-app` in your current working directory.

    * Inside `my-hapi-app`, it will generate a basic Hapi.js application, usually including:

    * `package.json`: With Hapi as a dependency and a basic `start` script.

    * `index.js` or `server.js`: The main application entry point, setting up and starting the Hapi server.

    * Potentially a `routes` directory, a `plugins` directory, and other common Hapi project conventions.

    #### 2. Navigate into the new project and install dependencies

    After creating the project, you'll typically want to change into its directory and install the necessary Node.js packages.

    bash
    hapi new my-api-service
    cd my-api-service
    npm install
    # or yarn install

    **Explanation:**

    * `hapi new my-api-service`: Generates the project structure.

    * `cd my-api-service`: Changes the current directory to the newly created project folder.

    * `npm install` (or `yarn install`): Reads the `package.json` file and downloads all specified dependencies (like Hapi, Inert, Vision, etc., depending on the template) into the `node_modules` directory.

    #### 3. Start the newly created Hapi application

    Once dependencies are installed, you can usually start the server using the script defined in `package.json`.

    bash
    hapi new my-web-app
    cd my-web-app
    npm install
    npm start
    # or node index.js (if index.js is the main entry point)

    **Explanation:**

    * `npm start`: Executes the `start` script defined in your `package.json`. For a typical Hapi project generated by `hapi new`, this script usually runs the main server file (e.g., `node index.js`).

    * Upon successful startup, the server will likely be listening on a specified port (e.g., `http://localhost:3000`), and you can access it via your web browser or API client.

    Summary

    The `hapi new` command is a foundational tool for quickly getting started with Hapi.js development. It automates the initial project setup, allowing developers to immediately focus on building features rather than configuring the basic project structure and dependencies.