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

StackOverflow https://stackoverflow.com/questions/22761967

  •  24-06-2023
  •  | 
  •  

Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

  • See @mmocny's answer for why cca is a (global) npm package, despite not containing modules for use in JavaScript code.
  • This answer shows how global npm packages work.

npm packages installed globally, with -g, typically contain executables (whether binary or not) that are to be added to a folder expected to be in your system's $PATH in order to make them globally available in your shell (from the command line) - independently of Node.js.

As npm help folder puts it succinctly (emphasis mine):

  • Install it locally if you're going to require() it.
  • Install it globally if you're going to run it on the command line.

Global package installation roughly works as follows:

Note: What directory {prefix} represents varies by platform (e.g., /usr on Linux) - you can query its value with npm get prefix or npm prefix -g.
The default Node.js installations on Unix systems install to shared locations, so that root privileges (via sudo) are required for installing packages globally.
By contrast, if you are using Unix-based multi-version managers such as n or nvm, {prefix} may be a user-specific directory such as ~ or ~/.nvm/v0.10.28, so that root privileges are not required for installing packages globally.
The following description is based on Unix platforms, with differing behavior on Windows noted separately, where needed.

  • Global packages are installed in a package-specific subfolder of {prefix}/lib/node_modules - e.g., /usr/local/lib/node_modules.

  • Symlinks to the executables from the package's bin subfolder (typically; as defined in the "bin" property of the package's package.json file) are then created in {prefix}/bin, e.g., usr/local/bin - which is what makes them globally available, given that {prefix}/bin is assumed to be in the $PATH.

    • Windows: .cmd batch files are used in lieu of symlinks, and they are placed directly in {prefix} (which is added to %PATH% during installation). These batch files are created based on analyzing a given executable's shebang line and explicitly invoke it with the interpreter found there, allowing for a seamless cross-platform experience.
  • man pages from the package, if defined, are symlinked to {prefix}/share/man, e.g., /usr/local/share/man

    • Windows: man pages are not installed at all.

See also http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/

The -g option makes it a "global" package, which provide global commands. These are used for modules like build managers (for instance grup). These are modules that are used in the same way over multiple projects, therefore they are "global" modules.

I am not sure however why they would use the -g option for cca, since cca seems to give you project specific functionality, not something like project management.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top