Why you can't
Getting to display as npm install packageName -g
in the npmjs.com is not possible. you cannot impose people to use your module as global, actually npm does not recommend the use of global modules unless necessary.
Just like how global variables are kind of gross, but also necessary in some cases, global packages are important, but best avoided if not needed.
Additionally, even global modules are used locally in order to freeze package version if you are deploying to other servers etc.
for example, using istanbul for test coverage, you can use the package globally and its simpler to type istanbul test
however installing istanbul locally will freeze the version in the package.json file in order to keep it working regardless to updates.
You install it locally and write node node_modules/istanbul/index.js test
istanbul is just an example use-case here.
Displaying a warning:: preferGlobal
If your package is primarily a command-line application that should be installed globally, then set this value to true to provide a warning if it is installed locally.
It doesn't actually prevent users from installing it locally, but it does help prevent some confusion if it doesn't work as expected.
However
setting preferGlobal: true
is not enough to trigger a warning when installing locally.
in order to get the message you have to check the following checklist:
valid package.json file.
contains dependencies
object in the package.json
file (even if it was empty)
You do not see this message in these cases:
if you are in a non-npm project (there is no package.json
).
if you are in an npm package-project directory that happens to have the package already installed as a local dependency.
sources:
https://stackoverflow.com/a/28206992/5384679
https://github.com/npm/npm/issues/11652
https://nodejs.org/en/blog/npm/npm-1-0-global-vs-local-installation/