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
It's best to install locally when relying on a package from your module, such as Node. js. This is how npm install works by default. The grunt CLI package, for example, must be installed globally before it can be used as a command-line tool.
To install a module from npm globally, you'll simply need to use the --global flag when running the install command to have the module install globally, rather than locally (to the current directory). Note: One caveat with global modules is that, by default, npm will install them to a system directory, not a local one.
Install the dependencies to the local node_modules folder. In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package. By default, npm install will install all modules listed as dependencies in package. json .
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:
package.json
file in the current directory,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:
package.json
file).dependencies
and/or optionalDependencies
key - by contrast, keys devDependencies
and/or peerDependencies
alone do not trigger the warning).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 ]
You need to specify
"preferGlobal": true
in your package.json
.
Documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With