The `hapi new` command is used to scaffold a new Hapi.js project. It automates the creation of a new directory with a basic Hapi server structure, `package.json`, and other essential files, based on available templates.
hapi new <project-name> [options]- `<project-name>`: (Required) The name of the new project directory to be created. This will also be used as the project's name in `package.json`.
The Hapi CLI `new` command supports various options to customize the project creation process. Note that specific options might vary slightly depending on the version of the Hapi CLI.
- `--template <template-name>` or `-t <template-name>`: Specifies a particular project template to use. If not specified, the CLI typically defaults to a basic Hapi server setup. Common templates might include `basic`, `api`, `web`, etc., but these depend on what's available in the specific CLI version or configured plugins.
- `--install` or `-i`: Automatically runs `npm install` (or `yarn install` if `yarn.lock` is detected) after the project is created, installing all initial dependencies.
- `--git` or `-g`: Initializes a Git repository in the new project directory.
- `--plugin <plugin-name>` or `-p <plugin-name>`: (Less common for `new`, more for `add`) Adds a specific Hapi plugin to the generated project. This might be used to include common plugins like `inert` or `vision` during project creation.
1. **Create a basic Hapi project:**
This command will create a new directory named `my-first-hapi-app` with a minimal Hapi server setup.
hapi new my-first-hapi-app2. **Create a Hapi project and install dependencies immediately:**
This is very common, as it sets up the project and makes it ready to run without an additional command.
hapi new my-api-project --install3. **Create a Hapi project using a specific template (e.g., 'api') and initialize Git:**
If the CLI supports different templates, this allows for more opinionated project structures.
hapi new my-web-app --template api --git4. **Create a project, install dependencies, and initialize Git:**
hapi new awesome-app --install --gitWhen you execute `hapi new <project-name>`, the Hapi CLI performs the following actions:
1. **Creates a new directory**: A folder named `<project-name>` is created in the current working directory.
2. **Scaffolds project files**: Inside the new directory, it generates essential files and folders, typically including:
* `package.json`: Configures the project with its name, version, scripts, and dependencies.
* `index.js` or `server.js`: Contains the main Hapi server setup code.
* `routes/`: A directory for defining application routes.
* `lib/`: A directory for utility functions or business logic.
* `.gitignore`: (If `--git` option is used) A default `.gitignore` file to exclude common build artifacts and `node_modules`.
3. **Installs dependencies (optional)**: If the `--install` flag is provided, the CLI will run `npm install` or `yarn install` within the new project directory to download and set up all required Node.js modules defined in `package.json`.
4. **Initializes Git repository (optional)**: If the `--git` flag is provided, `git init` is executed, preparing the directory for version control.
The `hapi new` command significantly speeds up the initial project setup, ensuring a consistent and best-practice starting point for Hapi.js applications.