npm-init
Initialize a new Node.js project
TLDR
Initialize a new package with prompts
Initialize a new package with default values
Initialize a new package using a specific initializer
SYNOPSIS
npm init [-y|--yes] [--force|-f] [--scope
PARAMETERS
--yes, -y
Skips all prompts and uses default values to create package.json.
--force, -f
Forces initialization, potentially overwriting an existing package.json or bypassing errors.
--scope
When creating a scoped package, specifies the scope (e.g., `@myorg`).
--ignore-scripts
Prevents any scripts (e.g., `preinstall`) from being executed during the initialization process.
--private
Sets `private: true` in package.json to prevent accidental publication to the npm registry.
Uses a specific 'create-' package (e.g., `react-app` for `create-react-app`) to scaffold a project.
DESCRIPTION
The npm init command is a fundamental utility in the Node.js ecosystem, primarily used to establish or update the package.json file for a project. This crucial file acts as the manifest for any Node.js package, containing vital metadata such as its name, version, description, entry point, test commands, scripts, dependencies, author, and license information.
When executed without specific options, npm init operates interactively, prompting the user for various details to populate the package.json. This guided process ensures that the generated file accurately reflects the project's characteristics. If a package.json already exists in the current directory, npm init will attempt to update it, merging new inputs with existing data.
A common and highly efficient usage is npm init -y (or --yes), which bypasses all interactive prompts. Instead, it automatically generates a package.json file populated with sensible default values. These defaults are often derived from the current directory name, npm's configuration, and standard conventions (e.g., version "1.0.0", main "index.js"). This non-interactive mode is particularly useful for quickly bootstrapping new projects or for automated build processes.
Beyond simple initialization, npm init can also leverage 'initializer' packages (e.g., npm init react-app, npm init vite). When used in this manner, it functions as a convenient shortcut for npm create
CAVEATS
Using npm init -y provides speed but may generate a package.json that requires manual adjustments for specific project needs.
When utilizing an
npm init only creates the package.json file; it does not install any dependencies or project scaffolding beyond what the initializer provides. This typically requires a subsequent npm install command.
THE `PACKAGE.JSON` FILE
The package.json file is the heart of any Node.js project managed by npm. It contains crucial metadata, scripts, and dependency declarations. npm init is the primary tool to generate this file, ensuring consistency and proper project management.
INITIALIZER PACKAGES (`CREATE-*`)
When npm init is followed by an initializer (e.g., npm init vite), it acts as a shorthand for npm create
HISTORY
The npm package manager, along with its core commands like npm init, was developed by Isaac Z. Schlueter and first released in January 2010. npm init has been a foundational command since early versions, providing a standardized and interactive way for developers to set up new Node.js projects. Over time, its functionality expanded to include the convenient --yes flag for quick initialization and the integration of initializer packages (e.g., npm init ), significantly streamlining the scaffolding of various application types.
SEE ALSO
npm install(1), npm publish(1), npm config(1), package.json(5)