w3resource

npm-hook and npm-init cli options


In the previous tutorial we looked at explore, help and help-search cli options. In this tutorial we will examine how npm-hook and npm-init work.

npm-hook

The npm-hook command is used to manage registry hooks.

Synopsis

npm hook ls [pkg]
npm hook add <entity> <url> <secret>
npm hook update <id> <url> [secret]
npm hook rm <id>

Example

If you need to add a hook that watches a package for changes, run following command:

$ npm hook add lodash https://example.com/ my-shared-secret

To add a hook that watches packages that belongs to the user subtask, you should the following command:

$ npm hook add ~substack https://example.com/ my-shared-secret

If, you want to watch packages that are in the @npm scope, you should run:

To list all the hooks for the browserify package, for example you will need to run:

$ npm hook ls browserify

To update an existing hooks' url, run:

$ npm hook update id-deadbeef https://my-awesome-website.here/

Finally, to remove a hook, you need to run:

$ npm hook rm id-deadbeef

Description

This command will enable you to manage npm hooks, including the adding, removing, listing and updating of hooks.

Hooks enable you to configure URL endpoints that will be notified whenever a change happens to any of the supported entity types. There are three types of entities that can be watched by hooks, namely: packages, owners, and scopes.

If you need to create a package hook, you simply need to reference the package name.

If you want to create an owner hook, you should prefix the owner's name with ~(as in, ~yourself).

If you need to create a scope hook, you should prefix the scope name with @ (as in, @yourscope).

The hook id that is used by update and rm are the IDs listed in npm hook ls for that particular hook.

The shared secret is sent along the URL endpoint so that you can verify that the request came from your own configured hook.

npm-init

You should use the npm-init command when you want to create a package.json file in a location.

Synopsis

npm init [--force|-f|--yes|-y|--scope]
npm init <@scope> (same as `npx <@scope>/create`)
npm init [<@scope>/]<name> (same as `npx [<@scope>/]create-<name>`)

Examples

The following command will create a new React-based project using the create-react-app:

$ npm init react-app ./my-react-app

To create a new esm-compatible package using thre create-esm:

$ mkdir my-esm-lib && cd my-esm-lib
$ npm init esm ?yes

To generate a plain old package.json using legacy init:

$ mkdir my-npm-pkg && cd my-npm-pkg
$ git init
$ npm init

To generate it without having it ask any questions:

$ npm init ?y

Description

The npm init <starter> could be used to set up a new or existing npm package.

The starter used in this case is an npm package named create-<starter>, which can be installed using npx, and then its main bin executed -presumably creating or updating the package.json and running any initialization-related operations.

The init command will be transformed to a corresponding npx operations follows:

  • npm init foo -> npx create-foo
  • npm init @usr/foo -> npx @usr/create-foo
  • npm init @usr -> npx @usr/create

All additional option will be passed directly to the command, hence, npm init bar -hello will map to npx create-bar -hello

If you omit the initializer (by calling just npm init), init falls back to legacy init behavior. it asks you a bunch of questions and then it will write a package.json for you. It attempts to make reasonable guesses based on the existing fields, dependencies and options you selected. It is strictly additive, hence it will keep any fields and values that were already set. Use -y/ --yes to skip the questionnaire. If you pass -scope, it creates a scoped package for you.

Previous: Npm-explore, npm-help and npm-help-search cli options
Next: Npm-install, npm-install-ci-test and npm-install-test cli commands