Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is use of npm to install global packages that don't even get used in Node applications justified?

My knowledge of npm is this:

It is a package manager for Node.js applications. What this means is that when you need someone else's library/package for your node application you can conveniently use npm to get those dependencies.

But I recently came across this command:

npm install -g cca.

Then I enter cca in my terminal and now it starts some program.

Now my question is how can npm install packages that can be accessed via the terminal? I mean all packages installed by npm should be accessible by node application code (in JavaScript). This confuses me.

like image 753
batman Avatar asked Mar 31 '14 12:03

batman


People also ask

What is npm and how to use it?

The node package manager (npm) installs the packages required in a JavaScript project ​and provides a useful interface on which these packages can work. The npm install command allows the user to install a package. There are two types of installation: The following command is used to install packages using npm.

How do I install a package globally using NPM?

Installing a package globally allows you to use the code in the package as a set of tools on your local computer. To download and install packages globally, on the command line, run the following command: npm install -g <package_name>.

How to see which npm packages are outdated globally?

To see which NPM packages are outdated globally you can use this command: The command above also works for dev dependencies. To uninstall packages you simply use the uninstall command. Let’s uninstall lodash from our project

What is installing a package globally?

Installing a package globally allows you to use the code in the package as a set of tools on your local computer. To download and install packages globally, on the command line, run the following command:


2 Answers

npm install is a complicated command -- it has (at least) three major functions:

  1. From inside of a Node package (that is, a directory with a package.json file, or some subdirectory of it), running npm install installs all of that package's declared dependencies. It sticks these downloaded packages inside of a node_modules directory, and they are all available by the application's JavaScript code.

  2. Again, from inside of a node package, running npm install <package-name> will download and install a named package from the npm package repository. It will, again, place it in the node_modules directory, so that it is available to that application.

  3. From anywhere, running npm install -g <package-name> will download and install a named package globally. This means that it gets installed to your system's node_modules directory, and is available for all node packages to use.

The third usage, with -g, is also used for command-line utilities (as opposed to libraries). When installed with -g, packages can do things like installing new commands in /usr/local/bin, or installing man pages. These commands are then available to be run from a shell.

This is what cca does when you install it, and is the reason that we recommend installing with -g; so that you can use the cca command to create applications from anywhere, not because it is a kind of packaging utility.

like image 194
Ian Clelland Avatar answered Sep 24 '22 17:09

Ian Clelland


Sounds like your primary question is not how, but why?

The distinction here is between a node package vs a node module.

Only *module*s are meant to be require()ed by other node applications, and not all packages on npm need be modules. There are very many useful node packages that are only indirectly related to node. E.g., gulp or grunt or cordova or cca, etc.

These answers come (reworded) directly from the npm faq

For cca specifically, we hope to have a node module in the future, so the question of "why npm" is just forward thinking. Additionally, cca is a downstream distribution of cordova (just like phonegap) which was always hosted on npm, and we wanted to continue that heritage.

like image 37
mmocny Avatar answered Sep 20 '22 17:09

mmocny