npm

javascript 包管理器

选择 CLI 版本:

概要

Synopsis

npm

注意:此命令对工作区无感知。

Note: This command is unaware of workspaces.

版本

Version

10.4.0

描述

Description

npm 是 Node JavaScript 平台的包管理器。它将模块放置到位,以便 node 可以找到它们,并智能地管理依赖冲突。

npm is the package manager for the Node JavaScript platform. It puts modules in place so that node can find them, and manages dependency conflicts intelligently.

它具有极强的可配置性,可以支持各种用例。最常见的是,你使用它来发布、发现、安装和开发 node 程序。

It is extremely configurable to support a variety of use cases. Most commonly, you use it to publish, discover, install, and develop node programs.

运行 npm help 以获取可用命令的列表。

Run npm help to get a list of available commands.

重要

Important

npm 预配置为默认使用 npm 在 https://registry.npmjs.org 的公共注册表。使用 npm 公共注册表须遵守 https://npm.nodejs.cn/policies/terms 提供的使用条款。

npm comes preconfigured to use npm's public registry at https://registry.npmjs.org by default. Use of the npm public registry is subject to terms of use available at https://npm.nodejs.cn/policies/terms.

你可以将 npm 配置为使用你喜欢的任何兼容注册表,甚至可以运行你自己的注册表。使用他人的注册表受其使用条款的约束。

You can configure npm to use any compatible registry you like, and even run your own registry. Use of someone else's registry is governed by their terms of use.

介绍

Introduction

你可能获取 npm 因为你想安装东西。

You probably got npm because you want to install stuff.

你很可能想要在任何 node 程序中运行的第一件事是 npm install 以安装其依赖。

The very first thing you will most likely want to run in any node program is npm install to install its dependencies.

你也可以运行 npm install blerg 来安装最新版本的 "blerg"。查看 npm install 了解更多信息。它可以做很多事情。

You can also run npm install blerg to install the latest version of "blerg". Check out npm install for more info. It can do a lot of stuff.

使用 npm search 命令显示公共注册表中可用的所有内容。使用 npm ls 显示你已安装的所有内容。

Use the npm search command to show everything that's available in the public registry. Use npm ls to show everything you've installed.

依赖

Dependencies

如果一个包使用 git URL 列出了依赖,npm 将使用 git 命令安装该依赖,如果未安装将生成错误。

If a package lists a dependency using a git URL, npm will install that dependency using the git command and will generate an error if it is not installed.

如果 npm 尝试安装的包之一是原生 node 模块并且需要编译 C++ 代码,则 npm 将使用 node-gyp 来完成该任务。对于 Unix 系统,node-gyp 需要 Python、make 和 GCC 等构建链。在 Windows 上,需要 Python 和 Microsoft Visual Studio C++。欲了解更多信息,请访问 node-gyp 仓库node-gyp 维基

If one of the packages npm tries to install is a native node module and requires compiling of C++ Code, npm will use node-gyp for that task. For a Unix system, node-gyp needs Python, make and a buildchain like GCC. On Windows, Python and Microsoft Visual Studio C++ are needed. For more information visit the node-gyp repository and the node-gyp Wiki.

目录

Directories

请参阅 folders 以了解 npm 放置内容的位置。

See folders to learn about where npm puts stuff.

特别是,npm 有两种操作模式:

In particular, npm has two modes of operation:

  • 本地模式:npm 将包安装到当前项目目录,默认为当前工作目录。包安装到 ./node_modules,bin 安装到 ./node_modules/.bin

    local mode: npm installs packages into the current project directory, which defaults to the current working directory. Packages install to ./node_modules, and bins to ./node_modules/.bin.

  • 全局模式:npm 将软件包安装到位于 $npm_config_prefix/lib/node_modules 的安装前缀中,bin 安装到 $npm_config_prefix/bin

    global mode: npm installs packages into the install prefix at $npm_config_prefix/lib/node_modules and bins to $npm_config_prefix/bin.

本地模式是默认设置。在任何命令上使用 -g--global 以改为在全局模式下运行。

Local mode is the default. Use -g or --global on any command to run in global mode instead.

开发者用法

Developer Usage

如果你使用 npm 开发和发布代码,请查看以下帮助主题:

If you're using npm to develop and publish your code, check out the following help topics:

  • json:制作一个 package.json 文件。见 package.json

    json: Make a package.json file. See package.json.

  • link:将你当前的工作代码链接到 Node 的路径中,这样你就不必每次进行更改时都重新安装。使用 npm link 来执行此操作。

    link: Links your current working code into Node's path, so that you don't have to reinstall every time you make a change. Use npm link to do this.

  • install:如果你不需要符号链接,最好安装一些东西。特别是,从注册表安装其他人的代码是通过 npm install 完成的

    install: It's a good idea to install things if you don't need the symbolic link. Especially, installing other peoples code from the registry is done via npm install

  • adduser:创建一个账户或登录。执行此操作时,npm 会将凭据存储在用户配置文件中。

    adduser: Create an account or log in. When you do this, npm will store credentials in the user config file.

  • publish:使用 npm publish 命令将你的代码上传到注册表。

    publish: Use the npm publish command to upload your code to the registry.

配置

Configuration

npm 是非常可配置的。它从 5 个地方读取其配置选项。

npm is extremely configurable. It reads its configuration options from 5 places.

  • 命令行开关:使用 --key val 设置配置。所有键都有一个值,即使它们是布尔值(配置解析器在解析时不知道选项是什么)。如果你不提供值 (--key),则该选项设置为布尔值 true

    Command line switches: Set a config with --key val. All keys take a value, even if they are booleans (the config parser doesn't know what the options are at the time of parsing). If you do not provide a value (--key) then the option is set to boolean true.

  • 环境变量:通过在环境变量中为名称添加前缀 npm_config_ 来设置任何配置。例如,export npm_config_key=val

    Environment Variables: Set any config by prefixing the name in an environment variable with npm_config_. For example, export npm_config_key=val.

  • 用户配置:$HOME/.npmrc 处的文件是一个 ini 格式的配置列表。如果存在,则对其进行解析。如果在 cli 或 env 中设置了 userconfig 选项,则将使用该文件。

    User Configs: The file at $HOME/.npmrc is an ini-formatted list of configs. If present, it is parsed. If the userconfig option is set in the cli or env, that file will be used instead.

  • 全局配置:在 ./etc/npmrc 找到的文件(相对于全局前缀,如果找到就会被解析。有关全局前缀的更多信息,请参见 npm prefix。如果在 cli、env 或用户配置中设置了 globalconfig 选项,则改为解析该文件。

    Global Configs: The file found at ./etc/npmrc (relative to the global prefix will be parsed if it is found. See npm prefix for more info on the global prefix. If the globalconfig option is set in the cli, env, or user config, then that file is parsed instead.

  • 默认值:npm 的默认配置选项在 lib/utils/config/definitions.js 中定义。这些不得更改。

    Defaults: npm's default configuration options are defined in lib/utils/config/definitions.js. These must not be changed.

有关更多信息,请参见 config

See config for much much more information.

贡献

Contributions

欢迎补丁!

Patches welcome!

如果你想提供帮助,但不知道该做什么,请阅读 贡献指导 并检查问题列表。

If you would like to help, but don't know what to work on, read the contributing guidelines and check the issues list.

缺陷

Bugs

当你发现问题时,请报告它们:https://github.com/npm/cli/issues

When you find issues, please report them: https://github.com/npm/cli/issues

请务必遵循模板和错误报告指南。

Please be sure to follow the template and bug reporting guidelines.

特性请求

Feature Requests

在我们的论坛上讨论新特性的想法:

Discuss new feature ideas on our discussion forum:

或者建议正式的 RFC 提案:

Or suggest formal RFC proposals:

也可以看看

See Also

npm 中文网 - 粤ICP备13048890号