LinuxCommandLibrary

npm-init

Initialize a new Node.js project

TLDR

Initialize a new package with prompts

$ npm init
copy

Initialize a new package with default values
$ npm init [[-y|--yes]]
copy

Initialize a new package using a specific initializer
$ npm init [create-react-app] [my-app]
copy

SYNOPSIS

npm init [-y | --yes] [--scope=@scope] [--force | -f] [--verbose] [--registry ] [--access ] [--template ]

PARAMETERS

-y, --yes
    Skip all the prompts and use default values. This is useful for automated setups.

--scope=@scope
    Create a scoped package. The scope must be associated with a user or organization.

--force, -f
    Overwrite existing files, including `package.json`.

--verbose
    Enable verbose logging.

--registry
    Override the default npm registry.

--access
    Set the access level of the package when publishing. Defaults to `restricted` for scoped packages and `public` for unscoped packages.

--template
    Use a specific template for the package, provided as an npm package name. It runs `npm exec init --` and passes remaining arguments to the initializer.

DESCRIPTION

The `npm init` command is a utility for creating a `package.json` file, which is fundamental for any Node.js project managed by npm. It guides the user through an interactive questionnaire to define the project's metadata, such as name, version, description, entry point, test command, git repository, keywords, author, and license. This information is stored in `package.json`, which describes the project and its dependencies. Using `npm init` ensures a structured and standardized approach to project creation, making it easier to share and manage Node.js projects.

While basic usage is interactive, `npm init` also supports non-interactive modes, especially useful in automated scripting or CI/CD pipelines. This can be achieved by providing default values or using the `-y` flag, which skips the questionnaire and uses pre-defined default values.

The command is essential for managing dependencies and publishing packages to the npm registry. A well-defined `package.json` facilitates consistent builds, version control, and dependency resolution.

CAVEATS

Be cautious when using the `--force` flag as it will overwrite your existing package.json without warning. Using default values with `-y` might not always be suitable and can require manual edits afterwards.

CUSTOM INITIALIZERS

npm init allows you to use custom initializers. By running `npm init `, npm will use the `create-` package to scaffold the project, letting you have custom logic and scaffolding. For example: npm init react-app my-app will use the create-react-app initializer.

HISTORY

The `npm init` command has been a core part of npm since its early days. It was designed to simplify the process of creating `package.json` files, which are crucial for managing Node.js projects. Over time, the command has evolved to include support for scoped packages, non-interactive modes, and templates, reflecting the growing complexity and sophistication of the Node.js ecosystem. It is actively maintained and updated as part of the npm CLI.

SEE ALSO

npm(1), npm-install(1), npm-publish(1), package.json(5)

Copied to clipboard