选择命令行版本:
🌐 Description
npm CLI 提供了多种机制,用于在特定命令、配置和环境中向终端用户显示不同级别的信息。
🌐 The npm CLI has various mechanisms for showing different levels of information back to end-users for certain commands, configurations & environments.
🌐 Setting Log File Location
所有日志都写入调试日志,如果命令执行失败,则打印该文件的路径。
🌐 All logs are written to a debug log, with the path to that file printed if the execution of a command fails.
日志目录的默认位置是在 npm 缓存中的一个名为 _logs 的目录。可以通过 logs-dir 配置选项更改此位置。
🌐 The default location of the logs directory is a directory named _logs inside the npm cache. This can be changed with the logs-dir config option.
例如,如果你想将所有日志写入当前工作目录,你可以运行:npm install --logs-dir=.。这在调试特定的 npm 问题时尤其有用,因为你可以使用不同的配置值多次运行命令,然后比较所有日志文件的差异。
🌐 For example, if you wanted to write all your logs to the current working directory, you could run: npm install --logs-dir=.. This is especially helpful in debugging a specific npm issue as you can run a command multiple times with different config values and then diff all the log files.
当日志文件数量超过 logs-max 时,logs-dir 上的日志文件将被删除,最旧的日志将优先删除。
🌐 Log files will be removed from the logs-dir when the number of log files exceeds logs-max, with the oldest logs being deleted first.
要完全关闭日志,请设置 --logs-max=0。
🌐 To turn off logs completely set --logs-max=0.
🌐 Setting Log Levels
loglevelloglevel 是一个全局参数/配置,可以设置以确定要显示的信息类型。
loglevel 的默认值是 "notice",但可用的日志有多个级别/类型,包括:
🌐 The default value of loglevel is "notice" but there are several levels/types of logs available, including:
"silent""error""warn""notice""http""info""verbose""silly"将显示当前设置级别之前的所有日志。
🌐 All logs pertaining to a level preceding the current setting will be shown.
🌐 Aliases
上面列出的日志级别有各种对应的别名,包括:
🌐 The log levels listed above have various corresponding aliases, including:
-d:--loglevel info--dd:--loglevel verbose--verbose:--loglevel verbose--ddd:--loglevel silly-q:--loglevel warn--quiet:--loglevel warn-s:--loglevel silent--silent:--loglevel silentforeground-scriptsnpm CLI 从 v7 开始隐藏 npm install 生命周期脚本的输出。值得注意的是,这意味着你将看不到那些可能使用“安装脚本”向你显示信息的包的日志/输出,也看不到你自己项目中在 package.json 中定义的脚本的输出。如果你想更改此行为并记录这些输出,可以将 foreground-scripts 设置为 true。
🌐 The npm CLI began hiding the output of lifecycle scripts for npm install as of v7. Notably, this means you will not see logs/output from packages that may be using "install scripts" to display information back to you or from your own project's scripts defined in package.json. If you'd like to change this behavior & log this output you can set foreground-scripts to true.
🌐 Timing Information
--timing 配置 可以设置,它可以实现几件事情:
🌐 The --timing config can be set which does a few things:
logs-dir此文件包含一个 timers 对象,其中键是正在计时的过程部分的标识符,值是完成该部分所用的毫秒数。
🌐 This file contains a timers object where the keys are an identifier for the portion of the process being timed and the value is the number of milliseconds it took to complete.
有时候获取时间信息而不向终端输出任何内容是有帮助的。例如,性能可能会受到向终端写入的影响。在这种情况下,你可以使用 --timing --silent,它仍然会写入时间文件,但在运行时不会向终端输出任何内容。
🌐 Sometimes it is helpful to get timing information without outputting anything to the terminal. For example, the performance might be affected by writing to the terminal. In this case you can use --timing --silent which will still write the timing file, but not output anything to the terminal while running.
🌐 Registry Response Headers
npm-noticenpm CLI 会读取并记录从配置的注册表返回的任何 npm-notice 头。第三方注册表可以利用此机制在发生依赖网络的请求时提供有用的信息。
🌐 The npm CLI reads from & logs any npm-notice headers that are returned from the configured registry. This mechanism can be used by third-party registries to provide useful information when network-dependent requests occur.
此标头未缓存,如果请求是从缓存中提供的,则不会记录。
🌐 This header is not cached, and will not be logged if the request is served from the cache.
🌐 Logs and Sensitive Information
npm CLI 会尽最大努力从终端输出和日志文件中屏蔽以下内容:
🌐 The npm CLI makes a best effort to redact the following from terminal output and log files:
然而,不应依赖此行为来确保所有可能的敏感信息都被编辑。如果你担心日志文件或终端输出中的机密信息,可以使用 --loglevel=silent 和 --logs-max=0 来确保不向终端或文件系统写入任何日志。
🌐 However, this behavior should not be relied on to keep all possible sensitive information redacted. If you are concerned about secrets in your log file or terminal output, you can use --loglevel=silent and --logs-max=0 to ensure no logs are written to your terminal or filesystem.
🌐 See also