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.
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.
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>.
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
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
is a complicated command -- it has (at least) three major functions:
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.
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.
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.
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.
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