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/or optionalDependencies
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/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
).
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 ]