问题
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.
I thought npm install yo
used to have such a warning but now it does not. At least I cannot see it.
My environment:
› npm --version
1.4.10
› node -v
v0.10.28
回答1:
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).
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.
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
):
The warning - npm WARN prefer global <pkgName>@<ver> should be installed with -g
- is only triggered, if:
- there is a valid
package.json
file in the current directory, - AND it contains a
dependencies
and/oroptionalDependencies
key whose value is an object (whether empty or not) - unless the package at hand happens to be contained therein.
Pragmatically, this means that you will NOT see the warning when running npm install <pkgName>
(i.e., without -g
) in the following scenarios:
- 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/oroptionalDependencies
key - by contrast, keysdevDependencies
and/orpeerDependencies
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 keydependencies
oroptionalDependencies
).
Note that even --loglevel silly
does not alter this behavior, so there is currently no way to enforce unconditional display of the warning.
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 ]
回答2:
You need to specify
"preferGlobal": true
in your package.json
.
Documentation
回答3:
preferGlobal
is now deprecated:
This option used to trigger an npm warning, but it will no longer warn. It is purely there for informational purposes. It is now recommended that you install any binaries as local devDependencies wherever possible.
来源:https://stackoverflow.com/questions/24245619/how-do-i-make-my-npm-package-show-npm-warn-prefer-global-when-installing-local