如何在Bash中禁止npm WARN已弃用的消息

9

我在Linux Mint 18.0上安装了几个版本的node.js,使用nvm进行管理。由于项目依赖,我需要将版本0.10设置为默认版本(nvm use 0.10)。因此,每次启动终端时,都会输出以下内容:

npm WARN deprecated This version of npm lacks support for important features,
npm WARN deprecated such as scoped packages, offered by the primary npm
npm WARN deprecated registry. Consider upgrading to at least npm@2, if not the
npm WARN deprecated latest stable version. To upgrade to npm@2, run:
npm WARN deprecated 
npm WARN deprecated   npm -g install npm@latest-2
npm WARN deprecated 
npm WARN deprecated To upgrade to the latest stable version, run:
npm WARN deprecated 
npm WARN deprecated   npm -g install npm@latest
npm WARN deprecated 
npm WARN deprecated (Depending on how Node.js was installed on your system, you
npm WARN deprecated may need to prefix the preceding commands with `sudo`, or if
npm WARN deprecated on Windows, run them from an Administrator prompt.)
npm WARN deprecated 
npm WARN deprecated If you're running the version of npm bundled with
npm WARN deprecated Node.js 0.10 LTS, be aware that the next version of 0.10 LTS
npm WARN deprecated will be bundled with a version of npm@2, which has some small
npm WARN deprecated backwards-incompatible changes made to `npm run-script` and
npm WARN deprecated semver behavior.

如何抑制这些消息?

4个回答

14

您可以通过环境变量来关闭或减轻NPM的输出信息:

npm_config_loglevel=silent npm version

这应该适用于你的 .bashrc(或者无论你在哪里调用 nvm):

npm_config_loglevel=silent nvm use 0.10

或者您可以全局设置环境变量

export npm_config_loglevel=silent

可以在此处找到不同的日志级别(链接),它们包括:

  

"silent", "error", "warn", "http", "info", "verbose", "silly"


谢谢,那个方法可行。有趣的是:export npm_config_loglevel=silent 可以运行,但 export NPM_CONFIG_LOGLEVEL=silent 却不行。通常我会将环境变量大写。你知道为什么这个不起作用吗? - Alexander Popov
1
@AlexPopov 我同意这也不是我所期望的。环境变量是区分大小写的,所以这是 npm 维护者的决定。 - Andy
1
将“npm_config_loglevel=silent nvm use 0.10”添加到我的.bash_profile文件中并不起作用。 - Inigo

6
你可以在你的项目、用户目录或全局中添加 .npmrc 文件。 四个相关文件如下:
- 每个项目的配置文件(/path/to/my/project/.npmrc) - 每个用户的配置文件(~/.npmrc) - 全局配置文件($PREFIX/etc/npmrc) - npm 内置配置文件(/path/to/npm/npmrc)
例如:
prefix=~/.npm-global
loglevel=error

更多信息在这里


4

0

更新2021:

(我的问题不是npm WARN deprecated信息,而是npm WARN npm信息,但仍然适用)

我在Ubuntu上使用npm v8,同时也使用nvm v0.33.11。由于个人原因,我必须使用node 14而不是当前版本的node 16。

我压制这个问题的解决方案:

npm WARN npm npm does not support Node.js v14.4.0
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm cannot make any promises that npm will work with this version.
npm WARN npm You can find the latest version at https://nodejs.org/

在文件$HOME/.nvm/nvm.sh中,
在第2155行,我更改了这个。
   NVM_NPM_PREFIX="$(npm config --loglevel=warn get prefix)"

转换成这个

   NVM_NPM_PREFIX="$(npm config --loglevel=silent get prefix)"

此外(也许这是可选的),我还更改了$HOME/.nvm/.npmrc,在其中添加了这一行。
loglevel=silent

正如在此SO帖子中其他答案所提出的建议。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接