Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make my NPM package show "npm WARN prefer global" when installing locally

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
like image 758
Varvara Stepanova Avatar asked Jun 16 '14 14:06

Varvara Stepanova


People also ask

Does npm install globally or locally?

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.

How install npm install globally?

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.

Is npm install global by default?

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 .


2 Answers

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 ]

like image 83
mklement0 Avatar answered Sep 28 '22 15:09

mklement0


You need to specify

"preferGlobal": true

in your package.json.

Documentation

like image 23
Ben Fortune Avatar answered Sep 28 '22 16:09

Ben Fortune