npm is included as part of the Node JS installation.
Npm - What can it do for you
Npm fulfills many package management functionalities. There's the
npm command line tool; plus there's npm Inc. the company that maintains these free services and also offers commercial options, like package access via API, private npm registries and user/group management controls, features that are often needed by larger organizations.
node_modules - The installation directories in the global and local scopes
When you run npm, the
npm executable always defaults to a specific local directory to store and search for packages:
node_modules. There can be two kinds of
node_modules directories: a global one that belongs to the overall Node JS installation or a local project one that's the present working directory where the
npm command is run. Listing 9-1 illustrates how to use the
npm list command to obtain a list of installed packages.
Listing 9-1. Npm how to get a list of installed packages with
npm list &
npm -g list
[user@laptop]$ npm list /www/ └── (empty) [user@laptop]$ npm -g list /home/admin/.nvm/versions/node/v14.5.0/lib └─┬ firstname.lastname@example.org ├── email@example.com ....
Listing 9-1 starts by invoking the
npm list under the
(empty), indicating there are no packages for this local project.
The second example in listing 9-1 uses the
-g flag to tell npm to look up installed packages at the global level. In this case, you can see the output for
npm -g list starts with
/home/admin/.nvm/versions/node/v14.5.0/lib -- indicating the global Node JS installation directory -- followed by a tree of installed packages which in this case are abbreviated.
Note The global directory
/home/admin/.nvm/versions/node/v14.5.0/libin listing 9-1 points to a Node version manager installation directory; this global directory will vary depending how you installed Node JS (e.g. with nvm or not).
Next, let's install a package on a local project directory, which is basically on any directory you please that doesn't have a Node JS project (e.g.
/www/). Listing 9-2 illustrates this process.
Listing 9-2. Npm install package and generated files and directory structure
[user@laptop]$ npm install typescript added 1 package, and audited 2 packages in 2s found 0 vulnerabilities [user@laptop]$ npm list www@ /www └── firstname.lastname@example.org [user@laptop]$ ls ├node_modules ├──┬typescript │ ├──AUTHORS.md │ ├──bin │ ├──CODE_OF_CONDUCT.md │ ├──CopyrightNotice.txt │ ├──lib │ ├──LICENSE.txt │ ├──loc │ ├──package.json │ ├──README.md │ └──ThirdPartyNoticeText.txt │ ├package.json └package-lock.json
Listing 9-2 begins by locally installing the TypeScript package with
npm install typescript. Next, the output shows this process was a success with the message:
added 1 package, and audited 2 packages in 2s found 0 vulnerabilities.
Because the process in listing 9-2 is for a local project install, this automatically creates a local
node_modules directory. Notice in listing 9-2 that after the package installation, if you run the same
npm list command from listing 9-1, the output now shows a single line with
email@example.com, indicating the TypeScript package in its 4.2.4 version was installed.
Next, listing 9-2 executes the
ls directory utility which outputs a
node_modules directory that in itself contains a
typescript directory with more directories & files. It's in this last
typescript folder, that the source code for the
typescript package is located.
Finally, equally important than the
node_modules, notice the output of the
ls directory utility also shows the files:
package-lock.json. These last two files are always generated (or updated) when a package is installed with npm.
Npm - The