Table of contents
4.2 Forwarding additional options
1、npm help
1.1 Command usage
npm help <term> [<terms..>]
别名: hlep
1.2 Description
If a topic is provided, the corresponding documentation page is displayed.
If the topic does not exist, or if multiple terms are provided, npm will run help-search
the command to find a match. Note that if help-search
a single topic is found, it will be run on that topic help
, so a unique match is equivalent to specifying the topic name.
1.3 Configuration
viewer
- Default: "man" on Posix, "browser" on Windows
- Type: string
Program for viewing help content.
Set to "browser"
view html help content in the default web browser.
2、npm help-search
2.1 Command usage
npm help-search <text>
2.2 Description
This command will search npm markdown documentation files for the provided terms and then list the results, sorted by relevance.
If only one result is found, it will display that help topic.
If npm help
the argument is not a known help topic, then it will be called help-search
. It is rarely necessary to call this command directly.
2.3 Configuration
long
- Default value: false
- Type: boolean
Display extended information in ls
, search
and .help-search
3、npm hook
3.1 Command usage
npm hook add <pkg> <url> <secret> [--type=<type>]
npm hook ls [pkg]
npm hook rm <id>
npm hook update <id> <url> <secret>
3.2 Description
Allows you to manage npm hooks npm hooks npm hooks including adding, removing, listing and updating.
Hooks allow you to configure URL endpoints that are notified whenever any of the supported entity types changes. Hooks can monitor three different types of entities: package, owner, and scope.
To create a package hook, simply quote the package name.
To create an owner hook, precede the owner name with ~
(eg ~youruser
).
To create a scope hook, precede the scope name with @
(eg @yourscope
).
update
and rm
the hook used id
is npm hook ls
the ID listed in for that particular hook.
The shared secret will be sent to the URL endpoint so you can verify that the request came from your own configured hook.
3.3 Example
Add a hook to watch a package change:
npm hook add lodash https://you-url.com/ secret
Add a hook to view substack
packages belonging to the user:
npm hook add ~substack https://you-url.com/ secret
Add a hook to monitor @npm
packages in scope
npm hook add @npm https://you-url.com/ secret
List all active hooks:
npm hook ls
List lodash
activity hooks for a package:
npm hook ls lodash
Update the url of an existing hook:
npm hook update id-deadbeef https://my-new-website.here/
Remove a hook:
npm hook rm id-deadbeef
3.4 Configuration
registry
- Default value: " https://registry.npmjs.org/"
- Type: URL
The base URL of the npm registry.
otp
- Default value: null
- Type: null or string
This is the one-time password from the two-factor authenticator. It is required when using npm access
publish or change package permissions.
If not set, and the registry fails to respond and asks for a one-time password, npm will prompt for a one-time password on the command line.
If you have a paid personal or organizational npm account , you can start using hooks now.
A total of 100 hooks can be configured per user, and how you use them is up to you: you can have all 100 on one package, or spread them out over 100 different packages. If you use a hook to monitor a scope, this is considered a single hook, regardless of how many packages are in the scope. You can view any open source package on the npm registry, as well as any private package you control (you will only receive hooks for packages you have permission to view).
4、npm init
4.1 Command usage
npm init <package-spec> (same as `npx <package-spec>`)
npm init <@scope> (same as `npx <@scope>/create`)
别名: create, innit
Initializing the project will generate a package.json file in the directory.
npm init <initializer>
Can be used to set up new or existing npm packages.
In this case, initializer
it's an create-<initializer>
npm package called npm-exec which will be installed by npm-exec and then execute its main bin - presumably creating or updating package.json
and running any other initialization-related operations.
The init command is converted into the corresponding npm exec
operation as follows:
npm init foo
->npm exec create-foo
npm init @usr/foo
->npm exec @usr/create-foo
npm init @usr
->npm exec @usr/create
npm init @[email protected]
->npm exec @usr/[email protected]
npm init @usr/[email protected]
->npm exec @usr/[email protected]
If the initializer is omitted (by calling npm init
), init will fall back to the old init behavior. It will ask you a bunch of questions and then write a package.json for you. It will try to make a reasonable guess based on the existing fields, dependencies, and selected options. It is strictly additive, so it will retain all fields and values that have been set. You can also use -y
/ --yes
skip Q&A entirely. If you pass it --scope
, it will create a scope package.
Note : If the user has installed
create-<initializer>
the package globally,npm init
it will be used. If you want npm to use the latest version or another specific version, you must specify it:
npm init foo@latest
# Get and run the latestcreate-foo
npm init [email protected]
# run specifically[email protected]
4.2 Forwarding additional options
Any additional options will be passed directly to the command and therefore npm init foo -- --hello
will be mapped to npm exec -- create-foo --hello
.
To better illustrate how options are forwarded, here is a more evolved example showing the options passed to the npm client and creating the package. The following two commands are equivalent:
npm init foo -y --registry=<url> -- --hello -a
npm exec -y --registry=<url> -- create-foo --hello -a
4.3 Example
Use create-react-app to create a new React-based project. create-react-app must be installed globally in advance:
npm init react-app ./my-react-app
Use create-esm create-esm to create a new esm
compatibility package:
mkdir my-esm-lib && cd my-esm-lib
npm init esm --yes
Generate a plain old package.json using legacy init:
mkdir my-npm-pkg && cd my-npm-pkg
git init
npm init
Initialize package.json without asking any questions:
npm init -y
4.4 Workspace support
workspace
New workspaces can be created within a project using configuration options. When used npm init -w <dir>
, the cli will create the expected folders and boilerplate files, while also adding package.json
"workspaces": []
references to project properties to ensure the newly generated workspace is set up correctly .
Given a project without a workspace, create a directory with a package.json file.
mkdir codetest && cd codetest
npm init -y
Generate a new workspace using the old version of init:
npm init -w packages/a
This will generate a new folder and package.json
files, as well as update your top level package.json
to add a reference to this new workspace:
Workspace init also supports npm init <initializer> -w <dir>
syntax, following the same set of rules explained in the initial description section of this page. Similar to the previous example of using create-react-app to create a new React-based project, the following syntax will ensure that the new React app is created as a nested workspace within the project and configured package.json
to recognize it:
npm init -w packages/my-react-app react-app .
4.5 Configuration
yes
- Default value: null
- Type: null or boolean
Automatically answer "yes" to any prompts npm may print on the command line.
force
- Default value: false
- Type: boolean
Various protections against undesirable side effects, common bugs, unnecessary performance degradation, and malicious input have been removed.
- Allow breaking non-npm files in global installations.
- Allow
npm version
commands to work on unclean git repositories. - Allows
npm cache clean
deleting cache folders. - Allows installation
engines
of packages that declare that they require a different version of npm. - Allows installation of packages with
engines
declarations requiring different versionsnode
, even if enabled--engine-strict
. - Allows
npm audit fix
installation of modules beyond your declared dependencies (including major changes to SemVer). - Allows unpublishing all versions of a published package.
- Allow conflicting peerDependencies to be installed in the root project.
npm init
Implicitly set when--yes
.- Allows destruction
npm pkg
of existing values. - Allows unpublishing an entire package (not just a single version).
If you don't have a clear idea of what you want to do, we strongly recommend you not to use this option!
scope
- Default: the scope of the current project (if any) or ""
- Type: string
Associate operations with scopes in the scope registry.
Useful when logging in or out of a private registry:
# log in, linking the scope to the custom registry
npm login --scope=@mycorp --registry=https://registry.mycorp.com
# log out, removing the link and the auth token
npm logout --scope=@mycorp
This will result in @mycorp
a mapping to the registry for future installation @mycorp/package
of the packages specified based on the pattern.
This will also cause npm init
a scope package to be created.
# accept all defaults, and create a package named "@foo/whatever",
# instead of just named "whatever"
npm init --scope=@foo --yes
workspace
- Defaults:
- Type: String (can be set multiple times)
Enables running commands in the context of the current project's configured workspace, while filtering by running only workspaces defined by this configuration option.
workspace
Valid values for the configuration are:
- workspace name
- The path to the workspace directory
- Path to the parent workspace directory (will cause all workspaces in that folder to be selected)
When setting for npm init
a command, you can set it to a folder for a workspace that doesn't yet exist, to create a folder and set it as a completely new workspace in the project.
This value is not exported to the child process's environment.
workspaces
- Default value: null
- Type: null or boolean
Set to true to run the command in the context of all configured workspaces.
Explicitly setting this to false will cause install
commands like this to ignore the workspace entirely. When not explicitly set:
- Commands run on
node_modules
the tree (install, update, etc.) link workspaces tonode_modules
folders. - Commands that perform other operations (test, execute, publish, etc.) will run on the root project unlessworkspace
one or more workspaces are specified in the configuration.
This value is not exported to the child process's environment.
workspaces-update
- Default value: true
- Type: boolean
If set to true, npm cli will run updates after operations that may change the node_modules
workspace installed into the folder.
include-workspace-root
- Default value: false
- Type: boolean
Include the workspace root when enabling the workspace for a command.
When false, workspace
specifying a single workspace via the configuration, or workspaces
all workspaces via a flag, will cause npm to run only on the specified workspace, rather than the root project.