如何使我的NPM软件包显示"npm WARN偏好全局"在本地安装时 [英] How do I make my NPM package show "npm WARN prefer global" when installing locally

查看:146
本文介绍了如何使我的NPM软件包显示"npm WARN偏好全局"在本地安装时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很难用Google搜索这个主题-太多的用户问题,我的想法是关于程序包开发的.我希望我的软件包的用户在非全局安装时看到"npm WARN首选全局" .

Hard to google the subj — too many user questions, mine is about package development. I want a user of my package see "npm WARN prefer global" when installing it not globally.

我以为npm install yo曾经有过这样的警告,但现在却没有.至少我看不到.

I thought npm install yo used to have such a warning but now it does not. At least I cannot see it.

我的环境:

  › npm --version
1.4.10
  › node -v
v0.10.28

推荐答案

Ben Fortune的答案指定了 npm软件包 author 可以将软件包指定为首选 global 安装(通过在package.json文件中添加键值对"preferGlobal": true).

Ben Fortune's answer specifies how an npm package author can designate a package as preferring global installation (by adding key-value pair "preferGlobal": true to the package.json file).

可悲的是,正如OP自己在评论中指出的那样,这不足以总是 触发对用户安装这样的软件包的警告本地 .

Sadly, as the OP herself points out in a comment, this is NOT enough to always trigger a warning for users installing such a package locally.

从npm 2.3.0开始,使用npm install <pkgName>(即不使用-g)在本地安装全局安装首选的软件包时,行为如下:

As of npm 2.3.0, the behavior is as follows when installing a global-installation-preferred package locally, using npm install <pkgName> (i.e., without -g):

警告-npm WARN prefer global <pkgName>@<ver> should be installed with -g-仅 触发,如果:

The warning - npm WARN prefer global <pkgName>@<ver> should be installed with -g - is only triggered, if:

  • 当前目录中有一个有效的package.json文件
  • 并且它包含一个dependencies和/或optionalDependencies,其值是一个对象(无论是否为空)-除非手头的包装恰好包含在其中.

实用上,这意味着在以下情况下运行npm install <pkgName>(即不使用-g时)时,您不会看到警告:

Pragmatically, this means that you will NOT see the warning when running npm install <pkgName> (i.e., without -g) in the following scenarios:

  • 不是npm软件包项目的目录中(没有package.json文件).
  • 在npm软件包项目目录中恰好完全没有运行时依赖项(没有dependencies和/或optionalDependencies键-相比之下,键devDependencies和/或peerDependencies单独 not 不会触发警告).
  • 在npm软件包项目目录中,该目录恰好已经安装了pkgName作为(按本地定义) runtime 依赖项(在键dependenciesoptionalDependencies中).
  • in a directory that's not an npm package project (no package.json file).
  • in an npm package-project directory that happens to have no runtime dependencies at all (no dependencies and/or optionalDependencies key - by contrast, keys devDependencies and/or peerDependencies alone do not trigger the warning).
  • in an npm package-project directory that happens to have pkgName already installed as a (by definition local) runtime dependency (in key dependencies or optionalDependencies).

请注意,即使--loglevel silly也不会更改此行为,因此,当前没有办法强制无条件显示警告.

Note that even --loglevel silly does not alter this behavior, so there is currently no way to enforce unconditional display of the warning.

将软件包指定为全局软件包确实会产生无条件副作用,但是:在npm注册表中( http ://npmjs.com ),右侧软件包中显示的安装命令为npm install <pkgName> -g;也就是说,它确实包含-g.
[更新:此功能在一段时间前已失效,但到2015年9月14日仍然失效-请参见 https://github.com/npm/newww/issues/1017 ]

Designating a package as global does have one unconditional side effect, however: in the npm registry (http://npmjs.com), the installation command shown in the sidebar on the right for such a package is npm install <pkgName> -g; i.e., it does include the -g.
[Update: This functionality broke some time ago and is still broken as of 14 Sep 2015 - see https://github.com/npm/newww/issues/1017 ]

这篇关于如何使我的NPM软件包显示"npm WARN偏好全局"在本地安装时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆